PHP MYSQL, How to show records in four columns , - php

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

Related

Total of column in displayed result in PHP

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

table output php and mysql not getting result

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

print data with auto increment in paging

i know it's a silly question, but it is creating a problem for me....
I want to print data from mysql with auto increment, but when i use the below code, it auto increments the value in the same page but i want it to be continued to the next pages also, here is my code
<?php
$perpage = 10;
$start = (isset($_GET['id'])) ? $_GET['id'] : 0;
$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM register where
r_bid='".$_SESSION["id"]."'"), 0);
$select = "SELECT * FROM register where r_bid='".$_SESSION["id"]."' LIMIT
$start,$perpage";
$result = mysql_query($select) or die(mysql_error());
$res = mysql_query("select * from regi_balic where b_id='".$_SESSION["id"]."'");
$row1=mysql_fetch_array($res);
$i=1;
while($row = mysql_fetch_array($result))
{
echo '<tr>
<td align="center" width=5%><font size=3>'.$i.'</font></td>
<td width=12%><font size=3>'.$row1['name'].'</font></td>
<td align="center" width=5%><font size=3><a href="edit_detail.phpid='.$row["r_id"].'
&cand_id='.$_SESSION["id"].'&email='.$row["email"].'">'.$row['name'].'</a></font></td>
<td align="center" width=5%><font size=3>'.$row['reference'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['age'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['occupation'].'</font></td>
<td width=12%><font size=3>'.$row['mob_no'].'</font></td>
<td width=2%><a href="process_del_client.php?id='.$row['r_id'].'"
onClick="returnConfirmSubmit(\'Are You sure ?\')"><img src = "images/delete.png">
</a></td>
</tr>';
}
$i++;
echo '</table>';
if($start == 0)
{
echo "<br>Previous Page ";
}
else
{
echo '<br><a href='."./view.php?id=" . ($start - $perpage) . '>'.
"PreviousPage".'</a> ';
}
if($start + $perpage >= $TotalRec)
{
echo " Next Page<br>";
}
else
{
echo ' <a href='."./view.php?id=" . ($start + $perpage) . '>'."Next Page".'
</a><br>';
}
?>
update value in a session variable and use that onto another page for updation .
Ex..
$_SESSION['value']=0
$_SESSION['value']=$_SESSION['value']++;
try changing
$i=1;
to
$i=1+$start;

PHP IF ELSE statement Issue

My If Statement Inside of my while loop Does not seem to Execute Properly. I think It might have something to do with my logic. To me, it seems like it should work but there must be something Im missing. I need the While loop to run and count each time it does. On the fourth loop, I need the code in the if statement to run but that never seems to happen. Can anyone offer a soloution please?
<?php
$input = $_GET['input'];//Note to self $input in the name of the search feild
$terms = explode(" ", $input);
$query = "SELECT * FROM content WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connecting to our mysql database
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
for($i=0; $i < $numrows; $i++){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$plink = $row ['plink'];
$views = $row ['views'];
if($i== 4){
echo '<td valign="top" "width="248" height="100%">
<table width="100%" border="0">
<tr>
<td align="center" valign="top"><a href='.$link.'>
<img src='.$plink.'width="200" height="151" vspace="5" />
<br><b><a href='.$link.'>'.$title.'</b></a>
<br><strong><span style="line-height:20px">Total views: '.$views.'</span></strong>
</td>
</tr>
</table>
</td><tr>';
}
else{
echo '<td valign="top" "width="248" height="100%">
<table width="100%" border="0">
<tr>
<td align="center" valign="top"><a href='.$link.'>
<img src='.$plink.'width="200" height="151" vspace="5" />
<br><b><a href='.$link.'>'.$title.'</b></a>
<br><strong><span style="line-height:20px">Total views: '.$views.'</span></strong>
</td>
</tr>
</table>'
;
}
}
}
}
else
echo "No results found for \"<b>$input</b>\"";
// disconnect
mysql_close();
?>
You're looping once for each row, but within your for loop, you have a while that is fetching all the rows. You need to get rid of the for loop.So instead of doing this:
for($i=0; $i < $numrows; $i++){
while ($row = mysql_fetch_assoc($query)){
you should do this:
$i = 0;
while ($row = mysql_fetch_assoc($query)){
$i++;
....
On the first iteration of your for loop $i == 1, so your condition won't execute. However, your while loop will empty the result set so that on the second and subsequent iterations of your for loop there are no rows to fetch. Your if condition isn't encountered again.

Defining HTML tables within PHP

I'm trying to get an HTML Table output in to a specific format via PHP after it grabs information from a MySQL Database.
In HTML, I can do this:
<table cellpadding="0" cellspacing="0" class="list">
<tr>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name1'">Name1</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name2'">Name2</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name3'">Name3</td>
</tr>
<tr>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name4'">Name4</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name5'">Name5</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name6'">Name6</td>
</table>
This works great and looks nice, I have it DIV'd up and formatting nicely.
However, I need to do this by pulling down the names from MySQL and then creating a new cell when needed, then after 3 cells, create a new row.
So far, I have this:
<?php
//PRESENTERS HERE
/* connect to the db */
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db, $connection);
/* show tables */
$result = mysql_query('SHOW TABLES', $connection) or die('cannot show tables');
echo '<h1>Presenters:</h1>';
while ($tableName = mysql_fetch_row($result))
{
$table = 'presenters';
/* Get the presenters*/
$result2 = mysql_query('SELECT presenter FROM ' . $table) or die('cannot show data from ' . $table);
}
if (mysql_num_rows($result2))
{
echo '<table cellpadding="0" cellspacing="0" class="list">';
while ($row1 = mysql_fetch_row($result2))
{
echo '<tr>';
foreach ($row1 as $key => $value)
{
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter=' . $value . '">', $value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
?>
But I'm unable to figure out how to get it to behave like the HTML one above. I also can't get the on click events to work.
The PHP form ends up with all entries on a row each, instead of three to a row.
Ahhh I see your problem for the entries in 3 rows and not one.
while($row1 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row1 as $key=>$value) {
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
Because each time you retrieve something from the database (your while loop statement) you are opening and closing rows in the table.
You need to have a counter started outside the while loop which counts how many rows you have and when you get to the number of columns you want, reset the counter and close off the table tags.
Something like this:
$columncount = 0;
while($row1 = mysql_fetch_row($result2)){
if($columncount == 0){
echo "<tr>";
}
foreach($row1 as $key=>$value) {
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
}
$coloumncount ++;
//assuming you want 3 columns in your table
if($columncount == 3) {
echo "</tr>";
$coloumncount = 0;
}
}
Won't solve your onclick events but it will format the table.
Good luck!
Is the first quote for the onclick attribute accidentally getting escaped on this line?
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
You're also using different CSS classes between your example static HTML, and the generated HTML in your PHP sample (class="list" vs. class="db-table").

Categories