I am wanting to make the data to show in 2 columns instead of one here is the code I currently use to show the data in 1 column:
<?php
include("config.php");
include("opendb.php");
$get_cats = mysql_query("SELECT * FROM `categories` ORDER BY `displayorder`") or die(mysql_error());
$get_info = mysql_query("SELECT * FROM `systeminfo`") or die(mysql_error());
$info = mysql_fetch_array($get_info);
while($cats = mysql_fetch_array($get_cats)) {
$get_rares = mysql_query("SELECT * FROM `rares` WHERE `catid`='".$cats['id']."'") or die(mysql_error());
echo("<h2>".$cats['name']."</h2><br>
<table width=\"100%\" border=\"0\">
<tr>
<td width=\"20%\" style=\"text-align:center\"><b>Image</b></td>
<td width=\"40%\" style=\"text-align:left\"><b>Item Name</b></td>
<td width=\"10%\" style=\"text-align:center\"><b>Value</b></td>
<td width=\"30%\" style=\"text-align:center\"><b>Last Updated</b></td>
</tr>
");
$color1 = $info[stripe1];
$color2 = $info[stripe2];
$row_count = 0;
while($rare = mysql_fetch_array($get_rares)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
?>
<tr>
<td width="5%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><img alt="" src="<?php echo("".$info[imagepath]."".$rare['image'].""); ?>"></td>
<td width="20%" style="text-align:left;background-color:#<?php echo $row_color; ?>"><?php echo $rare['name']; ?></td>
<td width="20%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><?php echo $rare['value']; ?> Credits</td>
<td width="10%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><?php echo $rare['lastedited']; ?></td>
</tr>
<?php
$row_count++;
}
echo("</table><br>
");
}
?>
Currently is shows as:
1
_________
2
_________
3
_________
4
_________
5
_________
6
_________
I would like it to show like this:
1 | 2
_________ | _________
3 | 4
_________ | _________
5 | 6
_________ | _________
This really doesn't have anything to do with MySQL at all. MySQL's just the source of the data. You'd want something like this:
$record = 0;
while($rare = mysql_fetch_array($get_rares)) {
if ($record % 2 == 0) {
echo "<tr>"; // if on an 'even' record, start a new row
}
echo "<td>{$rare['something']}</td>";
$record++;
if ($record % 2 == 0) {
echo "</tr>"; // close the row if we're on an even record
}
}
For what it's worth, I encorporated MarcB's code into your original code. You were already doing the mod with your row count. I think you want something like this:
<?php
include("config.php");
include("opendb.php");
$get_cats = mysql_query("SELECT * FROM `categories` ORDER BY `displayorder`") or die(mysql_error());
$get_info = mysql_query("SELECT * FROM `systeminfo`") or die(mysql_error());
$info = mysql_fetch_array($get_info);
while($cats = mysql_fetch_array($get_cats)) {
$get_rares = mysql_query("SELECT * FROM `rares` WHERE `catid`='".$cats['id']."'") or die(mysql_error());
echo("<h2>".$cats['name']."</h2><br>
<table width=\"100%\" border=\"0\">
<tr>
<td width=\"20%\" style=\"text-align:center\"><b>Image</b></td>
<td width=\"40%\" style=\"text-align:left\"><b>Item Name</b></td>
<td width=\"10%\" style=\"text-align:center\"><b>Value</b></td>
<td width=\"30%\" style=\"text-align:center\"><b>Last Updated</b></td>
<td width=\"20%\" style=\"text-align:center\"><b>Image2</b></td>
<td width=\"40%\" style=\"text-align:left\"><b>Item Name2</b></td>
<td width=\"10%\" style=\"text-align:center\"><b>Value2</b></td>
<td width=\"30%\" style=\"text-align:center\"><b>Last Updated2</b></td>
</tr>
");
$color1 = $info[stripe1];
$color2 = $info[stripe2];
$row_count = 0;
while($rare = mysql_fetch_array($get_rares)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
if ($row_count % 2 == 0) {
echo "<tr>"; // if on an 'even' record, start a new row
}
echo "<td width='5%' style='text-align:center;background-color:#$row_color;'><img alt='' src='{$info[imagepath]}{$rare['image']}'></td>
<td width='20%' style='text-align:left;background-color:#$row_color;'>{$rare['name']}</td>
<td width='20%' style='text-align:center;background-color:#$row_color;'>{$rare['value']} Credits</td>
<td width='10%' style='text-align:center;background-color:#$row_color;'>{$rare['lastedited']}</td>";
if ($row_count % 2 == 0) {
echo "</tr>"; // close the row if we're on an even record
}
$row_count++;
}
echo "</table><br/>";
}
?>
Related
Why my edit button only function for row 1 only in my table?
When I click other rows it still show data from row 1 but the id is changed.
When I update row 1 the data updates in the database.
//fetch the record to be updated
if (isset($_GET['edit'])){
$entry_id = $_GET['edit'];
$edit_state = true;
$rec = mysqli_query($db, "select r.room_id, r.room_name, s.time_date, s.entry_id, s.time_exam, s.course_code, s.course_enroll from room r, schedule_entry s where s.room_id = r.room_id");
$record = mysqli_fetch_array($rec);
$time_date = $record['time_date'];
$time_exam = $record['time_exam'];
$course_code = $record['course_code'];
$course_enroll = $record['course_enroll'];
$room_name = $record['room_name'];
$room_id= $record['room_id'];
$entry_id= $record['entry_id'];
}
?>
<?php
if (mysqli_num_rows($results)>0){
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td width="180"><?php echo $row['time_date']; ?></td>
<td width="70"><?php echo $row['time_exam']; ?></td>
<td width="200"><?php echo $row['course_code']; ?></td>
<td width="70"><?php echo $row['course_enroll']; ?></td>
<td><?php echo $row['room_name']; ?></td>
<td width="70">
<a class="edit_btn" href="entry.php?edit=<?php echo $row['entry_id']; ?>">Edit</a>
</td>
<td width="70">
<a class="del_btn" href="entryserver.php?del=<?php echo $row['entry_id']; ?>">Delete</a>
</td>
</tr>
<?php } }?>
you need to run the loop to update the ids,
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<td> <a class="edit_btn" href="entry.php?edit=
<?php echo $row['entry_id']; ?>">Edit</a></td>
<?php
}
}
?>
you have to evaluate your syntax, i think $row['entry_id'] have same value, so you must var_dump($row) form know all, like that
.... SOME CODE ....
<?php
if (mysqli_num_rows($results)>0){
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><? var_dump($row); ?>
</tr>
<?php } }?>
and after that i think you can solve your problem ...
Database schema looks like
ID Link_name Description
and wish The link name from the database to load in the table as
-----------------------------
Link_name1 | Link_name2 |
------------------------------
-----------------------------
Link_name3 | Link_name4 |
------------------------------
But the code
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error()); while($infoe = mysql_fetch_array( $datae )) {?>
<tr>
<td width="7%"> </td>
<td width="93%" class="main_text">
<h3><?php echo $infoe['Link_name']; }?><hr><br></h3>
</td>
</tr>
</table>
Output as
--------------
Link_name1 |
--------------
---------------
Link_name2 |
--------------
Not sure what are you asking for, but just a guess:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error());
$leftRight = 'left';
while($infoe = mysql_fetch_array( $datae )) {
if ( $leftRight == 'left') {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$leftRight = 'right';
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$leftRight = 'left';
}
}
if ( $leftRight == 'right') echo '<td></td><td></td></tr>'; ?>
</table>
EDIT If you need to format N column table:
$current = 1;
$columnLimit = 3; //you can set any number of column to output
while($infoe = mysql_fetch_array( $datae )) {
if ( $current == 1) {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
} elseif ( $current == $columnLimit) { {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$current = 1;
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
}
}
while ( $current!=1 && $current <= $columnLimit) {
if ($current != $columnLimit)
echo '<td></td>';
else
echo '<td></td></tr>';
$current++;
}
?>
Use the modulus operator (http://php.net/manual/de/language.operators.arithmetic.php)
With that you know if your current data entry is a odd or even number. so you can say something like: If your a even dataset then close the current row and open a new one.
Hello i have a table with some fields like
here i want make colors for table entire rows..means if ASR value is 75 to 100 should get one color and 50 to 75 should get another color and below 50 should get another color.
and here is my php code
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".(($attenedcalls/$row['totalcalls'])*100)."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
thanks in advance
You can try like this
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
$color = '';
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
$asr = (($attenedcalls/$row['totalcalls'])*100);
if($asr >= 75 && $asr <=100 ){
$color = 'red';
}else if($asr >= 50 && $asr < 75){
$color = 'cyan';
}else if($asr < 50){
$color = 'blue';
}
}
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr style='background-color : ".$color."'>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".$asr."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
[...]
while ($row = mysql_fetch_assoc($result)) {
$asrVal=(($attenedcalls/$row['totalcalls'])*100);
if($asrVal>=50 && $asrVal <=75) $class="from50to75";
if($asrVal>=75 && $asrVal <=100) $class="from75to100";
if($asrVal<50) $class="below50";
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr class='$class'>
[...]
then add:
<style>
tr.from50to75 td{background-color:red;}
tr.from75to100 td{background-color:green;}
tr.below50 td{background-color:blue;}
</style>
Modify your while loop so that you compute the ASR value before emitting the <tr> tag. Use that value to select a class according to the classification you have set up, and emit a tag of the form <tr class=foo> where foo is the class name you have selected. Then it’s just a matter of writing CSS rules for the classes, using class selectors like tr.foo.
(Provided that you have not set color on the td cells. If you have, you need to use selectors like tr.foo td to override such settings.)
How can I read the latest number from the following code
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] )
{
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
}
The result:
===========
NO | NAME | GROUP
---------------------------------------
===========
GROUP 1
---------------------------------------
===========
1 | A | 1
2 | B | 1
3 | C | 1
4 | D | 1
---------------------------------------
===========
GROUP 2
---------------------------------------
===========
1 | A | 2
2 | B | 2
3 | C | 2
I want to show the latest GROUP number, Group 1=4 and Group 2=3, can anyone help me with this?
Try this query:
SELECT s.*, c.count
FROM stock AS s
LEFT JOIN ( SELECT pbsid, COUNT(1) AS `count` FROM stock GROUP BY pbsid ) AS c
ON s.pbsid = c.pbsid
Now you will have the count for each group, which is what it seems you really want.
If you'd prefer to do all the work in PHP for some reason, you could do it like this:
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
$counts = array(); // make an array of counts for each group, keyed by pbsid
$rows = array(); // put all the data from SQL into an array
while ($row = mysql_fetch_array($hasil))
{
$rows[] = $row;
if (!isset($counts[$row[pbsid]]))
{
$counts[$row[pbsid]] = 0; // initialize it to 0
}
$counts[$row[pbsid]]++; // increment for each occurrence of a pbsid
}
foreach ($rows as $row)
{
$no++;
if($x != $row[pbsid] )
{
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b> Count = " . $counts[$row[pbsid]] . " </td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
}
Although, I think it's much simpler to just have this information calculated in your query.
This would work
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
$latest = array();
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] ){
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$key = 'Group'.$row['pbsid'];
$latest[$key] = $no;
}
print_r($latest);
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$last_no = array();
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] ){
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$last_no[] = $no;
}
print_r($last_no);
Change your query $query ="select MAX(no),bnama, pbsid from yourtablename GROUP BY pbsid ";
I am having a time tryng to pull ALL records out of a database. For example I have the following
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 50") or die(mysql_error());
// Variables to pull from the database
// I know the line below is the culprit now, so i must change the code below correct? ///
$returneddata = mysql_fetch_array($result);
///////////////////////////////////////////
$LatinName = $returneddata['Latin_Name'];
$CommonName = $returneddata['Common_Name'];
$Category = $returneddata['Category'];
$Type = $returneddata['Type'];
$Fruit = $returneddata['Fruit'];
$Flower = $returneddata['Flower'];
$MinHeight = $returneddata['Min_Height'];
$MaxHeight = $returneddata['Max_Height'];
$MinWidth = $returneddata['Min_Width'];
$MaxWidth = $returneddata['Max_Width'];
$Exposure = $returneddata['Exposure'];
$Comments = $returneddata['Comments'];
$SoilType = $returneddata['Soil_Type'];
$Zone = $returneddata['Zone'];
$PotSize = $returneddata['Pot_Size'];
$CostPrice = $returneddata['Cost_Price'];
$RetailPrice = $returneddata['Retail_Price'];
$ImageName = $returneddata['Image_Name'];
$ImageNameThumb = $returneddata['Image_Name_Thumb'];
$num_rows = mysql_num_rows($result); echo "$num_rows Rows\n";
while ($row = mysql_fetch_array($result)) {
echo " <tr>
<td align=\"center\" bgcolor=\"#000000\">
<p><img src=\"$row[Image_Name]\" style=\"width:120px;height:auto;\"></p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Latin_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Common_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Category]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Type]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Flower]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Comments]</p>
</td>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>Edit</p>
<p>Print</p>
</td>
</tr>
";
}
The issue i have is that it will pull all the records except for the first one. Every other record shows, there are only 5 records currently.
I am missing something silly I know it. I looked through questions and could not find the answer. Thanks
It could be because you're calling the mysql_fetch_array command twice.
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
You only need to declare mysql_fetch_array once, which is inside the while parameter.
This should be your code:
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {
echo " <tr>
<td align=\"center\" bgcolor=\"#000000\">
<p><img src=\"$row[Image_Name]\" style=\"width:120px;height:auto;\"></p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Latin_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Common_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Category]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Type]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Flower]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Comments]</p>
</td>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>Edit</p>
<p>Print</p>
</td>
</tr>
";
}
you didn't post valid/all your PHP - there's no mysql_query() call.
SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
line 2 there fetches a row and does nothing with it anyway, throwing away the first row. try this:
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {
After your question update, it looks like you want to pull out info from the first row, then loop over all the rows. Here's a way to do that:
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
$row = $rows[0];
$LatinName = $returneddata['Latin_Name'];
...
foreach ($rows as $row){
...
}