jueves, 26 de marzo de 2015

PHP explode example and foreach

<? $pieces = explode("|", 'one|two|thee|four'); foreach($pieces as $k=>$v){  echo '('.$v.")<br />"; } ?>


(one)
(two)
(thee)
(four)

domingo, 22 de marzo de 2015

consultando 2 tablas en un solo query

mysql> select audio_id,list_id from audio,list limit 0,1;
+----------+---------+
| audio_id | list_id |
+----------+---------+
|       18 |      79 |
+----------+---------+
1 row in set (0.00 sec)


mysql> select audio_unique,phone from audio,list  where audio_id=19 and list_id=94 limit 0,10;
+---------------+-------------+
| audio_unique  | phone       |
+---------------+-------------+
| 550f0e92e434e | 18097342323 |
+---------------+-------------+
1 row in set (0.00 sec)

sábado, 21 de marzo de 2015

HTML TABLE Highlited on mouse

<!DOCTYPE html>
<html>
<body>


<style style="text/css">
      .hoverTable{
        width:100%;
        border-collapse:collapse;
    }
    .hoverTable td{
        padding:7px; border:#4e95f4 1px solid;
    }
    /* Define the default color for all the table rows */
    .hoverTable tr{
        background: #b8d1f3;
    }
    /* Define the hover highlight color for the table row */
    .hoverTable tr:hover {
          background-color: #ffff99;
    }
</style>

<table class="hoverTable">
      <tr>
    <th>First Name</th>
    <th>Last Name</th>       
    <th>Points</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>       
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>       
    <td>94</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>       
    <td>80</td>
  </tr>


</table>



</body>
</html>

jueves, 19 de marzo de 2015

$_FILES[] array elements

$_FILES[] elements
Added By: Jim E.
IRC S/N: bawls
Last Revised: July 9, 2002

Description:
        Here are the elements that are passed in the $_FILES array after a file has been uploaded.
Script:

Assume the file field name was "file1"

$_FILES['file1']['tmp_name'] <== returns the name of the temporary file
$_FILES['file1']['name'] <== returns the name of the uploaded file
$_FILES['file1']['size'] <== returns the file size of the uploaded file (bytes)
$_FILES['file1']['type'] <== returns the mime type of the uploaded file

http://www.glodev.com/script_view.php?ScriptID=51

Ejemplos de tablas

http://www.w3schools.com/html/html_tables.asp


<!DOCTYPE html>
<html>

<head>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
}
th {
    text-align: left;
}
</style>
</head>

<body>

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>       
    <th>Points</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>       
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>       
    <td>94</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>       
    <td>80</td>
  </tr>
</table>

http://www.w3schools.com/html/html_tables.asp

miércoles, 18 de marzo de 2015

The following code should help beginner PHP/MySQL developers who are looking for an easy way to import a CSV or comma delimited file into a mysql database.  This example adds new contacts into the contacts table from an uploaded CSV file, populating the following three fields: (contact_first, contact_last, contact_email).

Click to view files:
I've included the PHP code below that 'does all the work'.  Click the 'import.php' link above to view the full code.
<?
    //get the csv file
    $file = $_FILES[csv][tmp_name];
    $handle = fopen($file,"r");
   
    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysql_query("INSERT INTO contacts_tmp (contact_first, contact_last, contact_email) VALUES
                (
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //
?>

############
<?php 
//connect to the database $connect mysql_connect("localhost","username","password"); mysql_select_db("mydatabase",$connect); //select the table
//
if ($_FILES[csv][size] > 0) {

    
//get the csv file
    
$file $_FILES[csv][tmp_name];
    
$handle fopen($file,"r");
    
    
//loop through the csv file and insert into database
    
do {
        if (
$data[0]) {
            
mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
                (
                    '"
.addslashes($data[0])."',
                    '"
.addslashes($data[1])."',
                    '"
.addslashes($data[2])."'
                )
            "
);
        }
    } while (
$data fgetcsv($handle,1000,",","'"));
    
//

    //redirect
    
header('Location: import.php?success=1'); die;

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>

</body>
</html>  




#################################
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `contacts`;
CREATE TABLE `contacts` (
  `contact_id` int(11) NOT NULL auto_increment,
  `contact_first` varchar(255) character set latin1 default NULL,
  `contact_last` varchar(255) character set latin1 default NULL,
  `contact_email` varchar(255) character set latin1 default NULL,
  PRIMARY KEY  (`contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
################
 
 
Jim,Smith,jim@tester.com
Joe,Tester,joe@tester.com 

Upload CSV and Insert into Database Using PHP/MYSQL


This script can be use to update data in database from local CSV file on user(admin) computer

Step 1 – Data Base Connection

At first we need to connect to database…
File Name: connection.php
1<?php
2$db = mysql_connect("Database", "username", "password") or die("Could not connect.");
3 
4if(!$db)
5 
6    die("no db");
7 
8if(!mysql_select_db("Databasename",$db))
9 
10    die("No database selected.");
11?>

Step 2 – upload page

Making connection to data base by calling the connection.php, clean the table of its old data, insert new uploaded data into table…
File Name: upload.php
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
5<title>Upload page</title>
6<style type="text/css">
7body {
8    background: #E3F4FC;
9    font: normal 14px/30px Helvetica, Arial, sans-serif;
10    color: #2b2b2b;
11}
12a {
13    color:#898989;
14    font-size:14px;
15    font-weight:bold;
16    text-decoration:none;
17}
18a:hover {
19    color:#CC0033;
20}
21 
22h1 {
23    font: bold 14px Helvetica, Arial, sans-serif;
24    color: #CC0033;
25}
26h2 {
27    font: bold 14px Helvetica, Arial, sans-serif;
28    color: #898989;
29}
30#container {
31    background: #CCC;
32    margin: 100px auto;
33    width: 945px;
34}
35#form           {padding: 20px 150px;}
36#form input     {margin-bottom: 20px;}
37</style>
38</head>
39<body>
40<div id="container">
41<div id="form">
42 
43<?php
44 
45include "connection.php"; //Connect to Database
46 
47$deleterecords = "TRUNCATE TABLE tablename"; //empty the table of its current records
48mysql_query($deleterecords);
49 
50//Upload File
51if (isset($_POST['submit'])) {
52    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
53        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
54        echo "<h2>Displaying contents:</h2>";
55        readfile($_FILES['filename']['tmp_name']);
56    }
57 
58    //Import uploaded file to Database
59    $handle = fopen($_FILES['filename']['tmp_name'], "r");
60 
61    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
62        $import="INSERT into tablename(item1,item2,item3,item4,item5) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";
63 
64        mysql_query($import) or die(mysql_error());
65    }
66 
67    fclose($handle);
68 
69    print "Import done";
70 
71    //view upload form
72}else {
73 
74    print "Upload new csv by browsing to file and clicking on Upload<br />\n";
75 
76    print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
77 
78    print "File name to import:<br />\n";
79 
80    print "<input size='50' type='file' name='filename'><br />\n";
81 
82    print "<input type='submit' name='submit' value='Upload'></form>";
83 
84}
85 
86?>
87 
88</div>
89</div>
90</body>
91</html>



http://coyotelab.org/php/upload-csv-and-insert-into-database-using-phpmysql.html

jueves, 12 de marzo de 2015

MYSQL PDO Conection

 <?php
/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = '1w3s';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=asterisk", $username, $password);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database<br />';

    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM users";
    foreach ($dbh->query($sql) as $row)
        {
        print $row['username'] .' - '. $row['password'] . '<br />';

        }

    /*** close the database connection ***/
    $dbh = null;
}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

mysql vs mysqli

http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php

MySQLi Procedural

Simple connection to the database using this extension mysqli.
<?php //conection: $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
//consultation:
$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
  echo
$row["name"] . "<br>";
}
?>
or

<?php //conection: $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
//consultation:
$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = mysqli_query($link, $query);
//display information:
while($row = mysqli_fetch_array($result)) {
  echo
$row["name"] . "<br>";
}
?> 



http://php.net/manual/en/function.mysqli-connect.php

http://php.net/manual/en/mysqli.construct.php 

domingo, 8 de marzo de 2015

PHP FILE UPLOAD

 <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="userfile" id="userfile">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>








###########################################
PHP UPLOAD SCRIPT


<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>


Permisos a la carpeta

chmod 777 /var/www/uploads/




http://www.w3schools.com/php/php_file_upload.asp





Edit Report a Bug HTTP authentication with PHP

< ?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}

?>

Comparisons of $x with PHP functions


Comparisons of $x with PHP functions
Expression gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE FALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1"; string FALSE FALSE TRUE TRUE
$x = "php"; string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE

PHP Logical Operators


Logical Operators
Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.

miércoles, 4 de marzo de 2015

php regular expresion

<?

$line = "Vi is the greatest word processor ever created!";
// perform a case-Insensitive search for the word "Vi"
if (preg_match("/\bVi\b/i", $line, $match)) :
  print "Match found!";
endif;


echo "<br>";


if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

?>

martes, 3 de marzo de 2015

Imprimiendo comilla doble en PHP

<?
echo  " asterisk -x \" sip show peers \"";

?>

Solo tenemos que poner un  \ delante de la comilla  ejemplo    <?php echo " \"hola"; ?>

imprime  "hola

Y esto   <?php   echo " \"hola\"";  ?>

"hola"
  

PHP Assignment by Reference

Assignment by Reference

Assignment by reference is also supported, using the "$var = &$othervar;" syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere.
Example #1 Assigning by reference
<?php
$a 
3;$b = &$a// $b is a reference to $a
print "$a\n"// prints 3print "$b\n"// prints 3
$a 4// change $a
print "$a\n"// prints 4print "$b\n"// prints 4 as well, since $b is a reference to $a, which has
              // been changed
?>
 
 
Esto es comoa acceso directo a  una variable 

domingo, 1 de marzo de 2015

Agregando valores automaticos a un arreglo he interactuando sobre ellos

<?php
$peers=array();

$peers[]=a;
$peers[]=b;
$peers[]=c;
$peers[]=d;


foreach ($peers as $extensions) {

echo $extensions."<br>";

$peers++;

}

?>

a
b
c
d