PHP - Trying to display Mysql results on new line - php

Trying to figure out how to make it so results stop displaying as one monolithic row but are instead each on their own line. Not having much luck thus far.
$result = mysql_query("SELECT *, ROUND(score) AS performance FROM images ORDER BY ROUND(score) DESC LIMIT 11,50");
while($row = mysql_fetch_object($result)) {
$top_ratings2[] = (object) $row;
}
<? foreach($top_ratings2 as $key => $image) : ?>
<td valign="top">
<b><?=$image->name?></b>
<b>Score:</b> <?=$image->score?>
<b>Wins:</b> <?=$image->wins?>
</td>
<? endforeach ?>

try using a table
<?php
$result = mysql_query("SELECT *, ROUND(score) AS performance FROM images ORDER BY ROUND(score) DESC LIMIT 11,50");
while ($row = mysql_fetch_object($result)) {
$top_ratings2[] = (object)$row;
}
?>
<table>
<thead>
<tr>
<td>Name</td>
<td>Score</td>
<td>Wins</td>
</tr>
</thead>
<tbody>
<?php foreach ($top_ratings2 as $key => $image) : ?>
<tr>
<td valign="top">
<?= $image->name ?>
</td>
<td valign="top">
<?= $image->score ?>
</td>
<td valign="top">
<?= $image->wins ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>

Related

How to highlight larger value in each table row?

Below is the base code I am trying for a product comparison.
How can I highlight the larger/small value ?
For example
Consider the price as an example.
attached result screenshot
attached excel example with highlight on value
$results = $mysqli->query('SELECT * FROM filterr where pid in ('.$pid1.''.$pid2.''.$pid3.''.$pid4.') ');
?>
// SETP FOUR : Displaying table values through HTML Table
<table>
<thead>
<tr>
<th>Details</th>
<th>Model 1</th>
<th>Model 2</th>
<th>Model 3</th>
<th>Model 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Manufacturer</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['product_brand']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Price</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['product_price']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Rating</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['score']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Battery</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['battery']; ?> </td>
<?php } ?>
<tr>
<tr>
<td>Storage</td>
<?php foreach ($results as $result){ ?>
<td> <?php echo $result['product_storage']; ?> </td>
<?php } ?>
<tr>
</tbody>
</table>
you can use simple if like :
.green{background-color:green;}
<?php if( $result['product_price'] > 5000 ){ //higher then ?>
<td class='green'>
<?php }else{ ?>
<td>
<?php } ?>
You can use same method for lower with another class.

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

Showing data in table row wise by using codeigniter php

How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>

doing a loop inside a loop using mysql ressources

I'm trying to do some loops in loop using mysql but it seems that there not interpreted
it returns only the first result then nothing even If I have many records in my database, It should return everything
below is my code that display the results
while ($cr = mysql_fetch_assoc($result)):
$i = 0;
++$i;
?>
<div style="width:200px" class="fif"><table style="width:100%">
<tr class="fbgreybox">
<td colspan="3" style="text-align: center;font-weight: bold">
<img src="images/calendar_2.png"> Séance du : <?php echo date('d-m-Y', strtotime($cr['record'])); ?>
</td>
</tr>
<?php
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$result = mysql_query($sql);
while ($exo = mysql_fetch_assoc($result)) :
?>
<tr class="fbinfobox">
<td colspan="3" style="text-align: left">
<img src="images/Sport-dumbbell.png"> ////<?php echo exerciseName($exo['exercise']); ?>
</td>
</tr>
<tr>
<td style="text-align: center;font-weight: bold">Séries</td>
<td style="text-align: center;font-weight: bold">Reps</td>
<td style="text-align: center;font-weight: bold">Poids</td>
</tr>
<?php
$rqt = "SELECT `set_number`, `reps`, `weight` FROM `workouts` WHERE `exercise` = {$exo['exercise']} AND `record` = '{$cr['record']}' AND `user` = {$_SESSION['userid']} ORDER BY `set_number`";
$result2 = mysql_query($rqt);
while ($detail = mysql_fetch_assoc($result2)):
?>
<tr>
<td>
Série ////<?php echo $detail['set_number']; ?>
</td>
<td>
<?php echo $detail['reps']; ?>
</td>
<td>
<?php echo $detail['weight']; ?>
</td>
</tr>
<?php
endwhile;
endwhile;
?>
</table>
</div>
<?php
endwhile;
there is not mistakes in the code so I really do not understand why it is not interpreted.
anykind of help will be much appreciated.
You are using $result in both mysql_fetch_assoc() function. AVOID THAT.
In inner loop you can use another variable:
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$workoutsResult = mysql_query($sql);
while ($exo = mysql_fetch_assoc($workoutsResult )) :
If I were you, I would first loop through the first query and put it in an array.
Then you loop through this array, and search for the inner queries and also put in arrays inside the other array.
Then, you loop through them to build your table.
It'll make your code cleaner, and will avoid problems like the one you're having, the one #Suresh pointed out.

PHP foreach will only work once within my html tables

Full code:
<table id="table-1">
<thead>
<th>Group Member</th>
<th>Member's Balance</th>
</thead>
<tbody>
<?php foreach ($members as $mem) : ?>
<tr>
<td><?php echo $mem['nick_name']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form action="./?action=pay_member" method="post" id="pay_org_member_form" >
<table id="table-1">
<thead>
<th>Group member</th>
<th>Amount</th>
<th>Description</th>
</thead>
<tbody>
<tr>
<td>
<select name="member_ID">
<?php foreach ($members as $member) : ?>
<option value="<?php echo $member['member_ID']; ?>">
<?php echo $member['nick_name']; ?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>$<input type="input" name="amount" /></td>
<td><input type="input" name="description" /></td>
</tr>
</tbody>
The problem I'm having now is the original table stopped pulling the $member variable. Only the new one works. It seems like I can only use this once. Anyway to get around this?
$members is declared like so:
Ok, $members is declared like so:
$members = get_other_members_by_org($org_ID, $member_ID);
and
function get_other_members_by_org($org_ID, $member_ID) {
global $db;
$query = "SELECT members.member_ID, nick_name, email_address
FROM
(members INNER JOIN orgs_to_members ON orgs_to_members.member_ID = members.member_ID)
WHERE org_ID = '$org_ID' AND members.member_ID != '$member_ID'";
$members = $db->query($query);
return $members;
The first foreach consumes the query. Requery, or put the results into an array.

Categories