Using 2 while loops to get data from 2 tables - php

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.

Related

how to total all row in page that was sql LIMIT

i put limit in this code but how can i calculate the totalmin in this page?
here is my code
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
</table>
mysql_close();
i want to know how to total all the mins in 15 row with php code
Something like this may do the trick, if you want to show sum only
But you should NOT use mysql_ functions, and this code is not sequre from SQL Injects
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
$sum = 0;
$num = 0;
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
$num++;
$sum += $row['totalmin'];
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
if($num == 15){
echo "<tr>";
echo "<td><center>".$sum."</center></td>";
echo "</tr>";
}
}
?>
</table>
<?php
mysql_close();
You need to store $totalmin into your while loop and update it with new value if $row['totalmin'] is less then $totalmin.
Use something like this:
$totalmin = null;
while($row = mysql_fetch_array($result)) {
if (is_null($totalmin) || $row['totalmin'] < $totalmin) {
$totalmin = $row['totalmin'];
}
echo "<tr>";
echo "<td><center>" . $totalmin . "</center></td>";
echo "</tr>";
}
Sum up the minutes as you loop through the result:
$sum = 0;
while($row = mysql_fetch_array($result)){
$sum += floatval($row['totalmin']); //or intval()
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
echo $sum;
make sure you close your while loop

how to expand a category list imported from MySQL

I have imported a set of different values from two different tables in phpMyAdmin, one from Category table (where cat_id is a PK) and another from Item table; where (cat_id is a FK).
<?php
require ('config.php');
$que = "SELECT * FROM category";
$run = mysql_query($que);
$j = 0;
while($row = mysql_fetch_array($run))
{
$cat_idd[$j] = $row['cat_id'];
$cat_namee[$j] = $row['cat_name'];
$j++;
}
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
}
?>
Here is what I have so far:
Now how do I expand it? or for example, how do I show the two items that are stored in the category named Clothing on the same page or next? Since this code only helps me to view the number of items stored inside the database,
Here is my form:
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr bgcolor = 'yellow'>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr class = "odd">
<?php
require ('config.php');
$que = "SELECT * FROM category";
$run = mysql_query($que);
$j = 0;
while($row = mysql_fetch_array($run))
{
$cat_idd[$j] = $row['cat_id'];
$cat_namee[$j] = $row['cat_name'];
$j++;
}
I thought of using an array function to store the value inside a category i.e Clothing(2) => 2.
function array_insert(&$array, $position, $insert)
{
if (is_int($position)) {
array_splice($array, $position, 0, $insert);
} else {
$pos = array_search($position, array_keys($array));
$array = array_merge(
array_slice($array, 0, $pos),
$insert,
array_slice($array, $pos)
);
}
}
$arr = array();
$arr[] = 2;
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]'";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
array_insert($arr, 0, "$run");
// echo $arr[0];
}
$que2 = "SELECT * FROM item WHERE cat_id = '1'"; //instead of using '1' i wish to use the one user clicks on!
$res2 = mysql_query($que2);
while($row = mysql_fetch_array($res2))
{
$i_id = $row['item_id'];
$i_namee = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_namee; ?></td>
<td><?php echo $i_price; ?></td>
<td><input type="checkbox" name="addcart" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php } ?>
<br><br>
</table>
<input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart" style="float:right; margin-top: 30px; margin-right: 10px;">Add to Cart</button>
The above image is the result what I want, this one is specific for Clothing(2) only. I want it to change as the user clicks on something else for example, Shoes(1).
You've issued the correct query to get all of the data information about an item. However, instead of getting the contents of each field, you've elected only to count the number of rows affected. By using mysql_fetch_array() you actually get the contents of each field and you can display the results.
Consider the following example based on your code:
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
while($row = mysql_fetch_array($result)){
//Change ['item_name'] and ['item_description'] to match your DB schema.
echo "item name: $row['item_name']";
echo "item description: $row['item_description']";
}
}
$row is an array (that is both associative and numerically indexed) containing all the data you are looking for. In my example, I assume the items table has a field named item_name and item_description. Your table probably has different columns names and you will need to change it to match your column names.
More info on mysql_fetch_array():http://php.net/manual/en/function.mysql-fetch-array.php
Finally, you should know that the mysql extension is deprecated as of PHP 5.5. You should strongly consider moving to either mysqli or PDO.
More info on mysqli: http://php.net/manual/en/book.mysqli.php
More info on PDO: http://php.net/manual/en/book.pdo.php
So after spending almost a day or two I have finally got a way out of this and I'm posting the answer here so if anyone else comes here looking for it.
<?php
require ('config.php');
$que = "SELECT * FROM category";
$run = mysql_query($que);
$j = 0;
while($row = mysql_fetch_array($run))
{
$cat_idd[$j] = $row['cat_id'];
$cat_namee[$j] = $row['cat_name'];
$j++;
}
for($i = 0; $i < count($cat_idd); $i++)
{
$que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
$result = mysql_query($que2);
$run = mysql_num_rows($result);
echo "<a href='c.php?id=$cat_idd[$i]'>".$cat_namee[$i]."(".$run.")</a><br><br>";
}
?>
<?php
require('config.php');
if(isset($_GET['id']))
{
$cat_id = $_GET['id'];
$que = "SELECT * FROM item WHERE cat_id = '$cat_id'";
$run = mysql_query($que);
?>
<br><br>
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?> />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>

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>

PHP - MySQL query with Pagination

How would I go about making a pagination script for this MySQL & PHP query.
if (isset($_GET['c'])) {
$c = $_GET['c'];
}
$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");
WHILE($datarows = mysql_fetch_array($query)):
$id = $datarows['id'];
$category = $datarows['category'];
$code = $datarows['code'];
endwhile;
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
WHILE($datarows_cat = mysql_fetch_array($query2)):
$title = $datarows_cat['title'];
$description = $datarows_cat['description'];
$imgurl = $datarows_cat['image_name'];
$category = $datarows_cat['category'];
$views = $datarows_cat['view_count'];
$pagename = $datarows_cat['pagename'];
$featured = $datarows_cat['featured'];
if ($featured =="1") {$f = "<img src='http://my-site.com/images/star.png' width='13px' title='Featured Game' /> Featured"; } else {$f = "";}
if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }
if (strlen($description) > 95) {
$desc= substr($description,0,95);
$desmod = "$desc...<br/>- Read More";
}
else {$desmod = "$description";}
echo "$f - $title - $desmod<br/>";
endwhile;
And when I visit http://my-site.com/categories/Action for instance, The code looks up that category in my category table, then once it gets the unique code for that category, It runs another query to find all games in another table with that category code. Currently, however, I have 200+ games loading for a single category which causes a great amount of loading time.
Thanks for your help!
First of all find out how many games are there for a specific category
change the line
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
to
$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);
Add following after it
$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);
if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);
Then display the numbers of pages where you want
<ul id="pagination">
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><?php echo $i;?></li>
<?php
}
?>
</ul>
Use CSS for pagination this will do the trick
//database connation
<?php
$conn = new mysqli("localhost", "root", "","database_name");
?>
<!DOCTYPE html>
<html>enter code here
<head>
<title>View Student Details</title>
<h1 align="center"> Student Details </h1>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="bootstrap/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<form>
<table class="table table-striped">
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
<th>Gender</th>
<th>Birth of Date</th>
<th>Contact No.</th>
<th>Action</th>
</tr>
<?php
$count=0;
if(isset($_GET['page_count']))
{
$count=1;
$page_count=$_GET['page_count']-1;
$count=$page_count*10;
}
$q="SELECT * from student_detail LIMIT $count,10";
$result=$conn->query($q);
$j=0;
while($data=$result->fetch_array())
{ $j=$j+1;
?>
<tr>
<td><?php echo $j ?></td>
<td><?php echo $data['std_id'] ?></td>
<td><?php echo $data['std_name'] ?></td>
<td><?php echo $data['std_class'] ?></td>
<td><?php echo $data['gender'] ?></td>
<td><?php echo $data['bod'] ?></td>
<td><?php echo $data['contact'] ?></td>
<td>
<div class="row">
<div class="col-sm-12">
Delete
Update
</div>
</div>
</td>
</tr>
<?php } ?>
</table>
<ul class="pagination">
<?php
$q="SELECT count(std_id) from student_detail";
$result=$conn->query($q);
$data=$result->fetch_array();
$total=$data[0];
$total_page=ceil($total/10);
if($total_page>1)
{
for($i=1;$i<=$total_page;$i++)
{
?>
<li class="active"><?php echo $i; ?></li>
<?php
}
}
?>
</ul>
</form>
<div class="col-sm-2"></div>
</div>
</div>
</body>
</html>
Pagiantion, it is working simple and easy
<?php
$sql = "SELECT COUNT(id) FROM contact_info";
$rs_result = $conn->query($sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
echo $total_records;
$previous = 1;
$total_pages = ceil($total_records / $limit);
$next = $_GET["page"] + 1;
$previous = $_GET["page"] - 1;
$pagLink = "<span>";
if($previous ==0)
{
$prev = "<a href='javascript:void(0);' >Previous</a>";
}
else
{
$prev = "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$previous."' style='color:black;'>Previous</a>";
};
echo $prev;
"</span><div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .= "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$i."'>".$i."</a>";
};
echo $pagLink;
$nex = "<span><a href='http://homeacresfinefurniture.com/all-queries.php?page=".$next."' style='color:black;'>Next</a></span>";
echo $nex;
";
</div>";
?>
Get all data from database.
$limit = 1;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM contact_info ORDER BY id desc LIMIT $start_from , $limit";
$page = 1;
$limit = 10;
$offset = ($limit * $page) - $limit;
$query = mysqli_query(
$connect,
"SELECT * FROM Games WHERE category = '$code' limit $limit offset $offset"
);

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

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;
}

Categories