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.
Related
I have a table 'tblexam' which contains State,city,candidate.I want to fetch the data from this table using where clause.I am able to fetch the data but i want to add the sum of Candidate at the last row(As Shown In Picture) how can i do that .
$sql="SELECT *
FROM tblexam
WHERE state='UP'";
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result) {
$cnt=$cnt+1; ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="left"align="left"><?php echo htmlentities($result->state);?></td>
<td class="center" align="left"><?php echo htmlentities($result->city);?></td>
<td class="center"align="left"><?php echo htmlentities($result->candidate);?></td>
<?php } ?>
<td class="center"align="left"><?php echo htmlentities($result->Total);?></td>
</tbody>
</table>
Add total while iterating through rows and display it outside the loop, Sample code is given below
$sql = "SELECT * FROM tblexam WHERE city='UP'";
$result = $conn->query($sql);
$numRows = $result->num_rows;
if ($numRows> 0) {
$total = 0;
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr>';
echo '<th>state</th>';
echo '<th>city</th>';
echo '<th>candidate</th>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
$total+=$row['candidate'];
echo '<tr>';
echo '<td>'.$row['city'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td>'.$row['candidate'].'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td>Total</td>';
echo '<td> </td>';
echo '<td>'.$total.'</td>';
echo '</tr>';
echo '<table>';
}
Before you start looping the data you can create a variable which you set to 0, inside the loop you can add the result's total to this variable, after the loop, the variable will contain the grand total:
$total = 0;
foreach($results as $result) {
$total += (int)$result->Total;
...
}
// $total = 1425
I am very new at using PHP and am trying to populate a table with data from a MySQL database, but the results that display are dotted all over the table and are not in their relevant columns at the top of the table.
Attached is a screenshot of where I need the data to be.
Here's the code.
Any help is appreciated.
<?php
$server = mysql_connect("xxxx", "xxxx", "xxxxx");
$db = mysql_select_db("xxxxxx", $server);
$query = mysql_query("SELECT * FROM stores");
?>
<html>
<head>
<title>Stores Tracker Dashboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p align="center"><font face="Arial"><strong>Stores Tracker Dashboard</strong></font></p>
<table align="center" border="1" width="90%">
<tr>
<td align="center"><font face="Arial"><strong>Not Started</strong></font></td>
<td align="center"><font face="Arial"><strong>In Transit</strong></font></td>
<td align="center"><font face="Arial"><strong>Awaiting Build</strong></font></td>
<td align="center"><font face="Arial"><strong>Being Built</strong></font></td>
<td align="center"><font face="Arial"><strong>On Return</strong></font></td>
<td align="center"><font face="Arial"><strong>Received Back</strong></font></td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {?>
<tr>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Not Started") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "In Transit") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Awaiting Build") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Being Built") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "On Return from CFC2") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
<td align="center"><font face="Arial"><?php if ($row['status'] == "Received") { echo $row['ticket'], ' (', $row['site'], ')'; }?></font></td>
</tr>
<?php } ?>
This one uses PDO.
Please take your time and learn what is going on there!
<?php
$host = "set_hostname_here";
$daba = "set_database_here";
$user = "set_loginname_here";
$pass = "set_password_here";
$notstarted = array();
$intransit = array();
$awaiting = array();
$built = array();
$cfc2 = array();
$received = array();
try {
//open the PDO-connection to a MySQL-host
$DBH = new PDO("mysql:host=$host;dbname=$daba", $user, $pass);
//error mode set to EXCEPTION (ERRMODE_SILENT or ERRMODE_WARNING are also possible)
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//execute the query
$STH = $DBH->query('SELECT status, ticket, site FROM stores');
//set default fetch mode to FETCH_ASSOC (if you want to try FETCH_OBJ)
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage();
}
//go through all results and assign them to the related arrays
while($row = $STH->fetch()) {
$data = $row['ticket'] . ' (' . $row['site'] . ')';
if ($row['status'] == "Not Started") { $notstarted[] = $data; }
if ($row['status'] == "In Transit") { $intransit[] = $data; }
if ($row['status'] == "Awaiting Build") { $awaiting[] = $data; }
if ($row['status'] == "Being Built") { $built[] = $data; }
if ($row['status'] == "On Return from CFC2") { $cfc2[] = $data; }
if ($row['status'] == "Received") { $received[] = $data; }
}
//close the connection
$DBH = null;
?>
<html>
<head>
<title>Stores Tracker Dashboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p align="center"><font face="Arial"><strong>Stores Tracker Dashboard</strong></font></p>
<table align="center" border="1" width="90%">
<tr>
<td align="center"><font face="Arial"><strong>Not Started</strong></font></td>
<td align="center"><font face="Arial"><strong>In Transit</strong></font></td>
<td align="center"><font face="Arial"><strong>Awaiting Build</strong></font></td>
<td align="center"><font face="Arial"><strong>Being Built</strong></font></td>
<td align="center"><font face="Arial"><strong>On Return</strong></font></td>
<td align="center"><font face="Arial"><strong>Received Back</strong></font></td>
</tr>
<?php
for ($i = 0; $i < max(count($notstarted),count($intransit),count($awaiting),count($built),count($cfc2),count($received)); $i++) {
echo "<tr>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($notstarted) > $i) echo $notstarted[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($intransit) > $i) echo $intransit[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($awaiting) > $i) echo $awaiting[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($built) > $i) echo $built[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($cfc2) > $i) echo $cfc2[$i];
echo "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\">";
if (count($received) > $i) echo $received[$i];
echo "</font></td>";
echo "</tr>";
}
?>
</table
</body>
</html>
I used the example of the answer by manassehkatz.
Most of the code relating the PDO connection is nearly copy/pasted from the link given by tadman ...
Good luck for your project.
Each row from the mysql query (yes, should be replaced with PDO, etc. but that is not directly related to the problem) reports the status of one stores record. The table is designed to report the values of several on each line - one of each status type. There are a few ways to do this. One way is to populate a set of arrays for the different status codes and then print out the arrays, once completed, as an HTML table. Something like this:
$notstarted = array();
$intransit = array();
$awaiting = array();
$built = array();
$cfc2 = array();
$received = array();
// Get all the data into the arrays
while ($row = mysql_fetch_array($query)) {
$data = $row['ticket'] . ' (' . $row['site'] . ')';
if ($row['status'] == "Not Started") { $notstarted[] = $data; };
if ($row['status'] == "In Transit") { $intransit[] = $data; };
if ($row['status'] == "Awaiting Build") { $awaiting[] = $data; };
if ($row['status'] == "Being Built") { $built[] = $data; };
if ($row['status'] == "On Return from CFC2") { $cfc2[] = $data; };
if ($row['status'] == "Received") { echo $received[] = $data; };
};
// Loop through all the arrays at once. # of rows will be max of size of all arrays
for ($i = 0; $i < max(count($notstarted),count($intransit),count($awaiting),count($built),count($cfc2),count($received)); $i++) {
print "<tr>";
print "<td align="center"><font face="Arial">";
if (count($notstarted) > $i) { print $notstarted[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($intransit) > $i) { print $intransit[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($awaiting) > $i) { print $awaiting[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($built) > $i) { print $built[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($cfc2) > $i) { print $cfc2[$i]; };
print "</font></td>";
print "<td align="center"><font face="Arial">";
if (count($received) > $i) { print $received[$i]; };
print "</font></td>";
print "</tr>";
};
I have the following: The result I am getting is the image attached. I would like the username to be listed only once and then the win, loose across the page. not a row for each result.
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ")
or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
$week = $row["win_loose"] ;
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: '.$row_color.';">';
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
you have to update your code. You are querying correctly but taking the data in a wrong way. You have to try associative array to solve your problem. Please try following codes-
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username[$row["username"]][] = $row["win_loose"];
}
foreach($username as $key=>$val){
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
foreach($val as $value){
echo '<td style="width: 50" align="center"><font size="2">'.$value.'</td>';
}
}
One more thing to mention, it is not a good practice to use inline css on every table column. You could use a class and get the css to the css file attached to that class.
Hope it helps...:)
You're pre-echoing weeks for which you don't have data yet. The idea is, print a cell for each week as long as it's referring to the same user:
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY username asc ")
or die(mysql_error());
$current_username = null;
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
//User changed
if ($current_username == null || $current_username != $username) {
if ($current_username != null) {
echo '</tr>'; //Had another user before so end the row
}
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: ' . $row_color . ';">';
echo '<td style="width: 100" align="center"><font size="2">' . $username . '</td>';
$current_username = $username;
}
$week = $row["win_loose"];
echo '<td style="width: 50" align="center"><font size="2">' . $week . '</td>';
}
echo '</tr>';
I have this code:
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); ?>
<table width="100%" border="1" cellspacing="0" cellpadding="3" >
<tr>
<td align="center"><strong><font size="2">Students</font></strong></td>
<td align="center"><strong><font size="2">Date</font></strong></td>
<td align="center"><strong><font size="2">Attendance</font></strong> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
$date = $rows['date']; $date2 = date("d-F-Y", strtotime($date));
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
?>
<tr>
<td align="center"><font size="2"><?php echo $rows['groupstudents']; ?></font> </td>
<td align="center"><strong><font size="2"><?php echo $date2; ?></font></strong> </td>
<td align="center"><font size="2"><?php echo $class1; ?></font></td>
</tr>
<?php
}
?>
which gives the below output.
Now my question is how to modify my code (use nested loops?!) so the output is:
Thank you kindly.
NB: Sorry, I do not have enough reputation to attach images. I have uploaded them on an external site.
It may not be the best solution, but I cannot think of something better right now.
In the pre-execution I create the grid you want, and in the layout this grid-array is displayed.
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql);
$dates = array();
$display = array();
while ($rows=mysqli_fetch_array($result)) {
if (!isset($display[$rows['groupstudents']])) {
$display[$rows['groupstudents']] = array();
}
if (!isset($dates[strtotime($rows['date'])])) {
$dates[strtotime($rows['date'])] = count($dates);
}
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
$display[$rows['groupstudents']][$dates[strtotime($rows['date'])]] = $class1;
}
echo '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
echo '<tr>';
echo '<td align="center"><strong><font size="2">Students</font></strong></td>';
foreach ($dates as $date => $reversedIndex) {
echo '<td align="center"><strong><font size="2">' . date("d-F-Y", $date) . '</font></strong></td>';
}
echo '</tr>';
foreach ($display as $student => $row) {
echo '<tr>';
echo '<td align="center"><font size="2">' . $student . '</font></td>';
foreach ($dates as $date => $index) {
echo '<td align="center"><font size="2">';
if (isset($row[$index])) {
echo $row[$index];
} else {
echo '';
}
echo '</font></td>';
}
echo '</tr>';
}
echo '</table>';
?>
Here is my code the records shows in four columns but if my records is blank it shows three balng images, any suggestions?
$query = mysql_query("SELECT * from rbf_events_images where event_id='".$_GET['id']."'");
echo '<table border="1">';
if(count(mysql_num_rows($query)>0)):
$tropentags='<tr>';
$troclosingtags='</tr>';
$formTags="";
$tdTags="";
$count=1;
while($row = mysql_fetch_array($query)){
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
if ($count>3)
{
$formTags.=$tropentags.$tdTags.$troclosingtags;
$tdTags="";
$count=0;
}
$count=$count+1;
}
if ($count>0)
{
for($i = 1; $i <= (4-$count) ; $i++)
{
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
}
$formTags.=$tropentags.$tdTags.$troclosingtags;
}
echo $formTags;
endif;
Thanks for your help!really appreciated!
I noticed that on lines like this one:
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
You are delimiting the string with single quotes ('), and you are also trying to embed a variable in the string that uses single quotes. I'm not sure how you did not get compile errors for that. I would switch to:
$tdTags= '<td align="left" valign="middle" class="td">' . $row['image'] . '</td>';
Here's what I usually do to put records in columns:
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * from rbf_events_images where event_id='$id'");
echo '<table border="1"><tbody><tr>';
if (mysql_num_rows($query) > 0) {
$count = 0;
while ($row = mysql_fetch_array($query)) {
if ($count && $count % 4 == 0) echo '</tr><tr>';
echo '<td align="left" valign="middle" class="td">'.$row['image'].'</td>';
$count++;
}
}
echo '</tr></tbody></table>';