How to make 4 items in a row using while loop? - php

I'm trying to display my products from the database. I'm trying to split it using the if 4%=0, but I can't get it to display 4 items in a row.
Here is my codes:
<table>
<tr>
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
?>
<td>
<table class="normal_text" align="center">
<tr>
<td><a href="product.php?product_id=<?php echo $id?>">
<img width="200px" height="133px" src="products/<?php echo $product_code?>.jpg" />
</a></td>
</tr>
<tr>
<td align="center" style="font-weight:bold"><?php echo $title;?></td>
</tr>
<tr>
<td align="center">$<?php echo $price;?></td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>

You've got the PHP logic before, rather than inside your HTML table output.
Try reorganizing like this:
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
?>
<table class="normal_text" align="center">
<tr>
<?php
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
// output product details -- note use of quotes to include PHP vars
$rowHTML = "<td><a href='product.php?product_id=$id'>";
$rowHTML .= "<img width='200px' height='133px' src='products/$product_code.jpg' />";
$rowHTML .= "</a><br/>";
$rowHTML .= "<strong>$title</strong>";
$rowHTML .= "$price</td>";
echo $rowHTML;
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
}
?>
</tr>
</table>

while($row=mysql_fetch_assoc($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
if ($split % 4 === 0) echo "\n<tr>";
?>
// ONLY <td> CODE </td> here. NO <tr> and NO </table>
<?php
if ($split % 4 === 3) echo "</tr>";
$split++;
}
?>
You are starting and ending at same time and then putting table for each.
You want to start <tr> when %4==0 and end </tr> when %4==3.

Rather than use modulus (%), just add a second variable called $cols, assign it to the amount of columns you want, and compare it. For example:
$rows = 1;
$cols = 4;
$display_all = mysql_query("SELECT * FROM products ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($rows==$cols){
echo '</tr><tr>';
$rows = 1;
}

Related

Quantity of every product outputs like 123. How can i put this in a table row table data

I am having a problem to convert my $quantity_total which is as example (113) from 3 different products.
I want it to be in a table like below.
I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.
<table>
<tr>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$quantity_all .=$quantity;
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
$_SESSION['total'] = $total;
$_SESSION['item-count'] = $item_count;
}
$_SESSION['quantity-all'] = $quantity_all;
Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.
I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.
As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555
http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8
<?php
$rows = '156165165489465131';
echo '<table>';
echo '<tr><th>Quantity</th></tr>';
foreach (str_split($rows) as $row) {
echo "<tr><td>$row</td></tr>";
}
echo '</table>';
I dont know if this Is what you want, but you can try something like:
<table>
<tr>
<td>Quantity</td>
</tr>
<?php
$characters = str_split( (string) $quantity_total);
foreach($characters as $char){
echo "
<tr>
<td> $char </td>
</tr>
";
}
?>
</table>

insert multiple shopping cart into database

I already search more about this topic solution "How to insert multiple item into database", but i can't understand. Hope somebody can help me !
shopping cart
{
while($row=mysqli_fetch_array($res)){
$total=($row['product_price']*$row['quantity']);
$tot = $tot + ($row['product_price']*$row['quantity']);
?>
<tr>
<td align="center"><img src="<?php echo $item['product_image'];?>" width="150px" height="150px" alt=""/></td>
<td><?php echo $row['product_name'];?></td>
<td><?php echo $row['quantity'];?></td>
<td>RM <?php echo $total;?></td>
<td style="text-align:center"><a href="delete_shopping_cart_item.php?id=<?php echo $row["id"];?>" onclick="return confirm('Are you sure you want to delete this item?');">
Remove
</a></td>
</tr>
<?php
}
if(isset($_POST['submit1']))
{
$customer = $_SESSION['login_user'];
$res = mysqli_query($mysqli,"SELECT * FROM shopping_cart where customer ='$customer'");
$row=mysqli_fetch_array($res);
$qty = $row['quantity'];
$product_name = $row['product_name'];
$rname = $_POST['rname'];
$raddress = $_POST['raddress'];
$rcontactno = $_POST['rcontactno'];
$total = $_SESSION['total'] ;
mysqli_query($mysqli,"insert into confirm_order (product_name,quantity,total_price,receiver_name,receiver_address,receiver_contact,customer)
values('$product_name','$qty','$total','$rname','$raddress','$rcontactno','$customer')");
$_SESSION['receiver'] = $rname;
}
}
Try this:
while ($row=mysqli_fetch_array($res)) {
$qty = $row['quantity'];
$product_name = $row['product_name'];
$rname = $_POST['rname'];
$raddress = $_POST['raddress'];
$rcontactno = $_POST['rcontactno'];
$total = $_SESSION['total'] ;
mysqli_query($mysqli,"insert into confirm_order (product_name,quantity,total_price,receiver_name,receiver_address,receiver_contact,customer)
values('$product_name','$qty','$total','$rname','$raddress','$rcontactno','$customer')");
}

getting value of array check box and textbox in table

i'm a beginner in php and i'm hoping that somebody here can help me in this.
i have a table that has a check box to select an item and a text box to indicate the quantity the person wants to borrow. i was wondering how i can retrieve both these data and then save them in my database.
here is a part of my code:
<td width="30">
<input id="optionsCheckbox" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['item_name']; ?></td>
<td align="center">
<img class="img-rounded" src="<?php echo $row['item_image'];?>" border="0" onMouseOver="showtrail('<?php echo $row['item_image'];?>','<?php echo $row['item_code'].": ".$row['item_name'];?> ',200,5)" onMouseOut="hidetrail()"></a></td>
<td><?php echo $row['item_quantity'] - $row['item_consumption']; ?> <?php echo $row['unit']; ?></td>
<td><input type="text" name="consume[]" pattern="[0-9]{1,4}"/></td>
here's what i have so far:
$id=$_POST['selector'];
$consume= $_POST['consume'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
echo $code; echo $name;
echo $id;
}
$x = count($consume);
for($y=0; $y < $x; $y++)
{echo $consume;
}
the echos are just for checking if the data goes through. i'm just making sure they do before i make a query for table insertion.
what i'm trying to do is to post them on another page, display them in a table (a la shopping cart) and then make the borrower fill out a form with his details.
ok, using gul's answer below, this is what i did:
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$consumes = substr($consumes,0,-1);
$query = $conn->query("select * from item where item_id IN($ids)")or die(mysql_error());
//table element
echo "<table border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Item Code</td>";
echo "<td width='150' align='center'>Item Name</td>";
echo "<td width='150' align='center'>Quantity</td>";
echo "</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table ="";
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="<td>".$consumes."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
however, when i select multiple items, only the last one shows up, but the consumes show up like this:
consume shows in one cell with single quotes and comma. help?
Try like this:
//first get the post
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
//$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$query = $conn->query("select * from item where item_id IN($ids))or die(mysql_error());
//table element
$table = "<table border='1'>";
$table.="<tr>";
$table.="<th>Code</th><th>Name</th>";
$table.="</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
ok, so i tried this and it worked. thanks to gul for helping me.
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$data = array();
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$row = array('selector'=>$id[$i],'consume'=>$consume[$i]);
//push in array
array_push($data,$row);
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
while ($row = $query->fetch()) {
$id = $row['id'];
}
?>
<tr>
<td><?php echo $code; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $consume[$i]; ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>

Display query result in a table

I have a MySQL query with over 50 return results. Now I need to display the results in a table with 3 rows and 3 columns.
Something like this:
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
I tried it with PHP like this:
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
//Create new output
$output = "<p>$name</p>";
$output .= "<p>$address</p>";
$output .= "<p>$content</p>";
//Add output to array
$mycontent[] = $output;
}
Then I am printing the content array in my table like this:
<tr>
<td><?php echo $mycontent[0]; ?></td>
<td><?php echo $mycontent[1]; ?></td>
<td><?php echo $mycontent[2]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[3]; ?></td>
<td><?php echo $mycontent[4]; ?></td>
<td><?php echo $mycontent[5]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[6]; ?></td>
<td><?php echo $mycontent[7]; ?></td>
<td><?php echo $mycontent[8]; ?></td>
</tr>
Using this code, I can only display 9 contents. My problem is that I want to display more content. I'm going to use pagination to display contents; something like 0-9, 10-18, 19-27 etc.
NOTE: I can do the pagination part.
I hope someone will give me the right direction for this.
Thank you.
try something like:
echo "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
}
echo "</table>";
this example will print in table all the result of the query.
if you want to limit only to some results, so limit the sql query.
for example:
$q = "SELECT name, address, content FROM mytable limit 50";
to get each content,name, address in TD, and 3 of mycontent(content, name, address) in a TR try this:
$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";
Try something like this. It places your values in an easy to use associative array. Then you just loop through.
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$values[] = array(
'name' => $row['name'],
'address' => $row['address'],
'content' => $row['content']
);
}
?>
<table>
<?php
foreach($values as $v){
echo '
<tr>
<td>'.$v['name'].'</td>
<td>'.$v['address'].'</td>
<td>'.$v['content'].'</td>
</tr>
';
}
?>
</table>
You can store in a variable if u r planning to use it in a function or you can just print it. Either way...
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
$str = "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$str .= "<tr>";
$str .= "<td>" . $row['name'] . "</td>";
$str .= "<td>" . $row['address'] . "</td>";
$str .= "<td>" . $row['content'] . "</td>";
$str .= "</tr>";
}
$str .= "</table>"
echo $str;
There you go!
use a for-loop to iterate your $mycontent-array:
EDIT: I was made aware that $mycontent is built differently than I first assumed:
$h = "<table>"; // we are going to build the table in $h
$maxcount = count($mycontent); // get the number of values within the array
for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...
$h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row
}
$h.= "</table>"; // close the table
echo $h; // echo it
---> live demo: http://3v4l.org/g1E1T
YOU CAN REFERENCE THIS CODE
<tbody>
<!-- <tr>
<td>Technology</td>
<td>Professor Abiola</td>
<td>wetech#school.com</td>
<td> Plot 2, Oriola road,</td>
<td>
<button type="button" class="btn btn-primary"> Edit</button>
<button type="button" class="btn btn-success">Update</button>
<button type="button" class="btn btn-danger">Delete</button>
</td>
</tr> -->
<?php
while($rows = mysqli_fetch_array($response)){
$faculty = $rows['faculty'];
$hod = $rows['hod'];
$email = $rows['email'];
// $location = $rows['location'];
echo "<tr>
<td>$faculty</td>
<td>$hod</td>
<td>$email</td>
<td>".$rows["location"]."</td>
<td>
<button type='button' class='btn btn-primary'>Edit</button>
<button type='button' class='btn btn-success'>Update</button>
<button type='button' class='btn btn-danger'>Delete</button>
</td>
</tr>";
}
?>
</tbody>
</table>

Using 2 while loops to get data from 2 tables

I'm trying to get an id from the users database so that I can link to about.php?id=$id1 and I'm using 2 while loops to do that. I end up getting what I want, about.php?id=$id1 but there are duplicate entries...
Here's the code.
<?php
require("connect.php");
require("header.php");
$max = 5; //amount of articles per page. change to what to want
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
//view the news article!
$id = isset($_GET['id']) ? $_GET['id'] : false;
if($id && is_numeric($id)){
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM blogdata WHERE id = '$id'");
while($r = mysql_fetch_array($sql))
{
echo $total;
$id = $r['id'];
$date = $r['date'];
$title = $r['title'];
$content = $r['content'];
$email = $r['author_email'];
$cat = $r['category'];
$author = $r['author'];
$query1 = mysql_query("SELECT * FROM users");
while ($row1 = mysql_fetch_array($query1)){
$id1 = $row1['id'];
echo "<center>
<table border='0' width='100%' cellspacing='10'>
<tr>
<td width='20%' valign='top'><div class='title'>$title</div>
<div class='info'><i>$date</i><br />
By <a href='$id1'>$author</a><br />
$cat</div>
</td>
<td width='80%' valign='top'>";
echo nl2br($content);
echo "</td>
</tr>
</table>
<hr />
<a href='index'>← Rewind.</a>
</center>";
}
}
}else{
//view all the news articles in rows
$sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC LIMIT ".$limits.",$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM blogdata"),0);
//the total number of pages (calculated result), math stuff...
$totalpages = ceil($totalres / $max);
while($r = mysql_fetch_array($sql))
{
$id = $r['id'];
$date = $r['date'];
$title = $r['title'];
$content = $r['content'];
$email = $r['author_email'];
$cat = $r['category'];
$author = $r['author'];
$query1 = mysql_query("SELECT * FROM users");
while ($row1 = mysql_fetch_array($query1)){
$id1 = $row1['id'];
echo "<center>
<table border='0' width='100%' cellspacing='10'>
<tr>
<td width='20%' valign='top'><div class='title'>$title</div>
<div class='info'><i>$date</i><br />
By <a href='$id1'>$author</a><br />
$cat</div>
</td>
<td width='80%' valign='top'>";
echo nl2br($content);
echo "</td>
</tr>
</table>
<hr />
</center>";
}
}
//close up the table
echo "</tr></table><center>";
$page = $_GET['p'];
for($i = 1; $i <= $totalpages; $i++)
{
if ( $page == $i ) {
echo "<b>$i</b>";
} else {
echo "<a href='?p=$i'>$i</a>";
}
if ($i < $totalpages)
echo " <font color=\"#666\">•</font> ";
}
}
echo "<br />";
require("footer.php");
?>
Couldn't fully get your question, but does SELECT DISTINCT column_name FROM table help you? It selects only distinct rows from a table.

Categories