php - Submit Button with multiple ID Inside Looping?(Shopping Cart) -


i want create shoping cart on php,

the code simple, when customer fill qty , click button add cart, code save product id , qty cart table. problem form inside looping. , how can id , qty button customer click.

the program this

enter image description here

and script this

<?php      if(isset($_post[add]))         {             $qty   = $_post[qty];             $harga = $_post[harga_asli];              $id    = $_post[id];              print_r($_post);         }           $kolom = 3;          $sql = "select *,format(harga,0)as harga_digit item";          $hasil = mysql_query($sql);         echo "<form method=post action=index.php>";         echo "<table>                <tr>";               $i = 0;               while($data=mysql_fetch_array($hasil))               {                  if($i >= $kolom)                  {                     echo "</tr><tr>";                     $i = 0;                  }                            $i++;                  echo "<td align='center'><br><a href='detailbarang.php?id=$data[id]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>                          rp. $data[harga_digit]<br>                           <input type='submit' name='add' id='add' value='add cart'>                          <input type='text' name='qty' id='qty' placeholder='qty' /><br>                          <input type='hidden' name='harga_asli' id='harga_asli' value='$data[harga]' /><br>                          <input type='hidden' name='id' id='id' value='$data[id]' />                          <br></td>";                 }//end of while          echo "<tr></table>";         echo "</form>";     ?> 

if fill qty , click add cart, last item can post data.

how post data customer choose?

im appreciated answer.

thanks

first, let's convert mysql mysqli. more explanation inside comments /* */:

<?php            $connection=mysqli_connect("yourhost","yourusername","yourpassword","nameofyourdatabase");  if(mysqli_connect_errno()){  echo "error".mysqli_connect_error();  }  $res=mysqli_query($con,"select * item");  while($row=mysqli_fetch_array($res)){  $nameofsubmitbutton=$row['id'];  if(isset($_post[$nameofsubmitbutton])){  $nameofproduct=$row['namaproduk']; $nameofnumbersubmitted=$nameofsubmitbutton."number"; $quantity=$_post[$nameofnumbersubmitted];  if(empty($quantity)){ echo "you wanted buy ".$nameofproduct."?<br>type in number can add cart."; }  else { mysqli_query($connection,"insert yourtable ('','') values ('$quantity','$nameofproduct')"); echo "you bought ".$quantity." of ".$nameofproduct; }  } /* end of if isset */  } /* end of while loop $res */  $kolom = 3;  $hasil = mysqli_query($connection,"select *,format(harga,0) harga_digit item"); /* sure query? */  echo "<form method=post action=''>"; /* submit on */ echo "<table><tr>";  $i = 0; /* set counter */ while($data=mysqli_fetch_array($hasil)) {  $id=$data['id'];  if($i >= $kolom){  echo "</tr><tr>"; $i = 0;  } /* end of if $i >= $kolom */     $i++;  echo "<td align='center'><br><a href='detailbarang.php?id=$data[id]'><img src='$data[img]' width='200' height='150'/><br>".$data[nama_produk]."</a><br>rp. ".$data[harga_digit]."<br>"; /* if echo variables, use ".$variable." */  $numbername=$id."number";  echo "<input type='number' name='$numbername' id='qty' placeholder='qty' /><br>"; /* change input type number */  /* no need hidden input */  echo "<input type='submit' name='$id' id='add' value='add cart'></td>"; /* change name of submit button corresponding id table */    } /* end of while loop */  echo "<tr></table>"; echo "</form>";   ?> 

i tried on local computer. should too.

here's sample screen shot. enter image description here


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -