Wednesday 30 March 2016

Find duplicate data in MySQL

<?php
        $con = mysql_connect('localhost', 'root', '');
        $db  = mysql_select_db('test', $con);

        $data = mysql_query('SELECT COUNT(price) as result, price as price FROM products GROUP BY price');

       while($r = mysql_fetch_array($data))
      {
 echo  $r['price'].'--'.$r['result'] .'<br>';
       }
?>

PHP - File Uploading

A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script.
Information in the phpinfo.php page describes the temporary directory that is used for file uploads as upload_tmp_dir and the maximum permitted size of files that can be uploaded is stated as upload_max_filesize. These parameters are set into PHP configuration file php.ini
The process of uploading a file follows these steps −
  • The user opens the page containing a HTML form featuring a text files, a browse button and a submit button.
  • The user clicks the browse button and selects a file to upload from the local PC.
  • The full path to the selected file appears in the text filed then the user clicks the submit button.
  • The selected file is sent to the temporary directory on the server.
  • The PHP script that was specified as the form handler in the form's action attribute checks that the file has arrived and then copies the file into an intended directory.
  • The PHP script confirms the success to the user.
As usual when writing files it is necessary for both temporary and final locations to have permissions set that enable file writing. If either is set to be read-only then process will fail.
An uploaded file could be a text file or image file or any document.

Creating an upload form

The following HTM code below creates an uploader form. This form is having method attribute set to post and enctype attribute is set to multipart/form-data

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $expensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>
      
      <form action="" method="POST" enctype="multipart/form-data">
         <input type="file" name="image" />
         <input type="submit"/>
      </form>
      
   </body>
</html>

Monday 21 March 2016

PHP - Alternating rows & column output

<?php

$items[334] = array( 'name' => 'L. Dalton', 'id' => 3456921 ); 
$items[234] = array( 'name' => 'B. Harris', 'id' => 563456 ); 
$items[212] = array( 'name' => 'R. Fox', 'id' => 6879879 ); 
$items[654] = array( 'name' => 'T. Marcus', 'id' => 14234 ); 
$items[865] = array( 'name' => 'M. Kitsap', 'id' => 79087 ); 
$items[875] = array( 'name' => 'P. McDonald', 'id' => 2454662 ); 
$items[675] = array( 'name' => 'J. Harvey', 'id' => 234545 ); 
$items[343] = array( 'name' => 'G. Steinmetz', 'id' => 8578768 ); 
$items[167] = array( 'name' => 'R. Stanley', 'id' => 34234 ); 
$items[899] = array( 'name' => 'C. Morris', 'id' => 546378 ); 
$items[932] = array( 'name' => 'S. Tan', 'id' => 578758 ); 


$counter = 1;
$limit = 4;
foreach($items as $item)
{
    if($limit == 4 && $counter == 1)
    {
        ?><div class="four-up"><?php echo "\n";
    }
    else if($limit == 3 && $counter == 1)
    {
        ?><div class="three-up"><?php echo "\n";
    }

    ?><div class="col col-<?php echo $counter; ?>"> <?php echo $item['name']; ?> <br /><?php echo $item['id']; ?></div><?php echo "\n";

    if($limit == $counter)
    {
        ?></div><?php echo "\n";
        $counter = 1;
        if($limit == 3) $limit = 4;
        else if($limit == 4) $limit = 3;
    }
    else
    {
        $counter++;
    }
} ?>

Print array with alternate colour in PHP

CODE

<?php
$arr = array(
    'Pune'   => '#ff0000',
    'Nashik' => '#0000ff',
    'Mumbai' => '#00ff00'
);

foreach ($arr as $something => $colour) : ?>
    <p style="color:<?php echo $colour ?>"><?php echo $something ?></p>
<?php endforeach ?>

OUTPUT

Pune

Nashik

Mumbai


Draw pyramid shape of stars in PHP using loop structure


OUTPUT
star_image


CODE


<?php
// problem 01 solution
for($i=5;$i>=1;$i--)
{
echo  str_repeat('*',$i);
echo "<br />";
}

echo "<hr />";

// problem 02 solution

echo '<p align="right">';
for($i=1;$i<=5;$i++)
{
echo  str_repeat('*',$i);
echo "<br />";
}
echo "</p>";

echo "<hr />";
// problem 03 solution

$j=1;

for($i=5;$i>=1;$i--)
{
echo  str_repeat("&nbsp;",$i-1);
echo  str_repeat('*',$j++);
echo "<br />";
}

?>

#php Algoritma membuat Piramida Bintang

1
2
3
4
5
6
7
8
9
for ($i = 0; $i < 5; $i++) {
 for ($j = 5; $j > $i; $j--) {
 echo '&nbsp;';
 }
 for ($k = 0; $k <= $i; $k++) {
 echo '*';
 }
 echo '<br/>';
 }


OUTPUT




     *
    **
   ***
  ****
 *****