Fetch mysql result in columns - php

I wish to divide my mysql fetched list in 2 columns. Eg.
A | D
B | E
C | F
I want to do it using table instead of < ul>< li>
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
while ( $result = mysql_fetch_assoc($sql) ) { ?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
</tr>
<?php } ?>
I have this structure.
NOTE - I wish to perform my solutions using PHP not by anysort of jquery and javascript.

<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$resultSet = array();
while ( $row = mysql_fetch_assoc($sql) ) {
$resultSet[] = $row;
}
$halfLength = count($resultSet) / 2;
for ($i = 0; $i < $halfLength; $i++) {
?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $resultSet[$i]["pid"], 1, 3); ?></td>
<td class="navigation"><?php echo fetch_table_poet( $resultSet[$halfLength + $i]["pid"], 1, 3); ?></td>
</tr>
<?php } ?>

Related

How to fetch data with same id in a row but different value in another column and display one month into table in php

I have the following data in MySQL table:
emp_id
date
shift
1001
2022-08-01
M
1001
2022-08-02
M
1001
2022-08-03
N
1002
2022-08-01
M
1002
2022-08-02
E
1002
2022-08-06
M
Output should be like this (One Month list show):
emp_id
2022-08-01
2022-08-02
2022-08-03
2022-08-04
2022-08-05
2022-08-06
1001
M
M
N
0
0
0
1002
M
E
0
0
0
M
My code is:
<?php
for($j = 1; $j <= date('t'); $j++)
{
$dat = str_pad($j, 2, '0', STR_PAD_LEFT). "-" . date('m') . "-" .
date('Y');
$d[] = date('Y'). "-" . date('m') . "-" . str_pad($j, 2, '0',
STR_PAD_LEFT);
$datess[] = str_pad($j, 2, '0', STR_PAD_LEFT)."<br>".date('D',
strtotime($dat));
}
?>
<?php
$link = mysqli_connect('localhost','root','','rostertest');
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Employee</th>
<?php foreach($datess as $dates) {?>
<th>
<?php echo $dates; ?>
</th>
<?php } ?>
</tr>
</thead>
<tbody id="tbl_body">
<?php
$i=0;
$get_sql = "select * from ros";
$run = mysqli_query($link,$get_sql);
while($row = mysqli_fetch_array($run)){
$date = $row['date'];
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row['emp_id']; ?></td>
<?php foreach($d as $da) { if($date==$da){?>
<td><?php echo $row['shift']; ?></td>
<?php }else{ ?>
<td><?php echo "0";?></td>
<?php }}?>
</tr>
<?php }?>
</tbody>
</table>
</div>
for these kind of data you need to have one other Table, for example Table.emp_date with a relation between (emp_id from ros) and (id from emp_date)
and you have to move 'emp_date' and 'shift' to this new table.
let me try to explain with your code too:
<?php
$i=0;
$get_sql = "select * from ros";
$run = mysqli_query($link,$get_sql);
while($row = mysqli_fetch_array($run)){
$get_sql2 = "select * from emp_date WHERE id=" . $row['emp_id'];
$run2 = mysqli_query($link,$get_sql2);
while($row2 = mysqli_fetch_array($run2)){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row2['emp_date']; ?></td>
<td><?php echo $row2['shift']; ?></td>
</tr>
<?php }}?>

Row number is not displaying first and last row number in paginated table in php

I am implementing a website in which admin has access to maintain stock. I am displaying stock from the database in html table. i have used pagination to display 10 records per page. I want that when i am on page 1 it displays "Showing 1-10 of total record" and when i am on page 2 it displays "Showing 11-20 of total record" and vice versa.
CODE:
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("login", $link);
$result = mysql_query("SELECT COUNT(*) FROM add_stock");
$row = mysql_fetch_row($result);
$num = $row[0];
?>
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Sr.No</th>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th><b>Image</b></th>
</tr>
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM add_stock ORDER BY id DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
$next_page = $page + 1;
$prev_page = $page - 1;
$i= $start_from;
$start=1;
while ($result=mysql_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $i+$start; ?></td>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td>
</tr>
<?php
$i++;
} ?>
</table>
<?php
$sql = "SELECT * FROM add_stock";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<div style="margin-left: 5px;">
<div class="pagination">
<?php
echo "<a href='viewstock.php?page=1'><b>«</b></a>";
if ($page==1) {
$page=1;
}
else{
echo "<a href='viewstock.php?page=$prev_page'><b>Prev</b></a>"; }
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='viewstock.php?page=".$i."'><b>".$i."</b></a>";
}
if ($page==$total_pages) {
$page=$total_pages;
}
else{
echo "<a href='viewstock.php?page=$next_page'><b>Next</b></a>"; }
echo "<a href='viewstock.php?page=$total_pages'><b>»</b></a> ";
?>
</div>
</div>
<p>Displaying <?php echo $start ?> - <?php echo $total_records ?> of Records: <?php echo $num ?></p>
This will work:
<p>Displaying <?php echo $start_from+1 ?> - <?php echo $start_from + $total_records ?> of Records: <?php echo $num ?></p>
as long as you:
1) Move the output below the code where the numbers you need are calculated
2) Don't re-use the variable $total_records after your SELECT * FROM add_stock. Instead give this instance of the variable a new name so it can't be confused with the earlier version, which means something different, and is needed for your output.
Please note also my first comment regarding your SQL Injection vulnerability, which you should take steps to fix as soon as possible.

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>

Divide list in two columns

I have list of values that I want to fetch from my database. So List is long I want to fetch list in two columns. How can I break it in 2 columns. Eg..
taking list of aphabets
A | B
C | D
E | F
G | H
I | J
K | L
IN this way. I used following approach...
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$i = 1;
$j = ceil($total_rows/2);
while ( $i <= $j ) {
$result = mysql_fetch_assoc($sql);
?>
<tr>
<? if($i%2 == 0) { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<? } elseif($i%2 != 0) { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<? } ?>
</tr>
<?php $i++; } ?>
Its giving me output in this way..
A | A
B | B
C | C
NOTE - I wish to do it using PHP not using any jquery or javascript.
It seems you are resetting your result every time you loop. Set the $results outside the while. The more accepted way to do this is:
while ($row = mysql_fetch_assoc($sql)){
}
Then test $i to see if it is divisible by 2 (meaning you have 2 columns and need to add the end of row and beginning of new row tags).
if($i%2 == 0){
echo '</tr><tr>';
}
try:
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$i = 0;
$total_rows = mysql_num_rows($sql);
while ( $result = mysql_fetch_assoc($sql) ) {
$i++;
?>
<? if($i&1 == 1) { ?>
<tr>
<td class="navigation"><?php echo $result["pid"]; ?></td>
<? } elseif($i&1 == 0) { ?>
<td class="navigation"><?php echo $result["pid"]; ?></td>
</tr>
<? }
if($i == $total_rows && $total_rows&1 == 1){ echo "</tr>"; } ?>
<?php } ?>
Edited: Bug fixes..
i think Colmn rows this will work:
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$iOne = 1;
$iTwo = floor($totalRows/2);
foreach($iOne=1,$iTwo=ceil($totalRows/2); $iTwo < $total_rows; $iOne++,$iTwo++){
?>
<tr>
<td class="navigation"><?php echo mysql_result($sql,$iOne,'pid') ?></td>
<td class="navigation"><?php echo mysql_result($sql,$iTwo,'pid') ?></td>
</tr>
<?php
}
?>
I would suggest jQuery columnizer as a solution to your problem http://welcome.totheinter.net/columnizer-jquery-plugin/
Finally I succeeded with code.
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$i = 1;
while ( $result = mysql_fetch_assoc($sql) ) { ?>
<?php if($i%2 == '1') { ?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<?php } elseif($i%2 == '0') { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
</tr>
<?php } ?>
<?php $i++; } ?>

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