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.
Related
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.
I am working on a small course registration system. I have a table called 20182019carryovertbl I select students carry over fron the table and its working well. I need to convert the selected info to an HTML form with submit button( This will go into another table 20192020coursestbl). How d i set name for my inputs
<table>
<thead>
<tr>
<th>Register course</th>
<th>Course Title</th>
<th>Corse Code</th>
<th>Unit</th>
<th>Lecturer</th>
</tr>
</thead>
<tbody>
<?php $sql = "SELECT * from 20182019carryovertbl WHERE matricno = '$matno' ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr>
<td> <input type="checkbox" name="reg1" value="1" class="custom-control-input"></td>
<td>
<?php echo htmlentities($result->coursetitle);?>
</td>
<td>
<?php echo htmlentities($result->coursecode);?>
</td>
<td>
<?php echo htmlentities($result->unit);?>
</td>
<td>
<?php echo htmlentities($result->lecturer);?>
</td>
</tr>
<?php $cnt=$cnt+1; }} ?>
</tbody>
</table>
you can use your unique key from database like Id:
<td>
<input type="checkbox" name="reg<?php echo $result->id;" value="1"class="custom-control-input">
</td>
and it's gonna be unique name
I have a table named 'entries' with the columns: originator, collaborator, engagement_partner, niche, and fee_potential. Some of the rows in the table have the same originator. I have a form that someone fills out, submits it to the database. I have tried using GROUP BY, but need some assistance on this.
My WordPress SQL statement: This will be used in the WordPress function file.
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM entries group by originator");
I need to display the data in a table format below. Basically, I need to group each entry based on Originator that are the same.
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>First Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
</tbody>
</table>
<br>
<br>
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Second Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
<tr>
<td><b>Second Originator</b></td>
<td>Company Name</td>
<td>Engagement Partner</td>
<td>Web</td>
<td>$40000</td>
</tr>
</tbody>
</table>
I suggest you fetch all the entries and group them with php like so;
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM entries");
$originators = [];
foreach($rows as $row)
{
$originators[$row->originator][] = $row;
}
then loop through the originators to fill the table, something like this:
<?php
foreach($originators as $originator => $entries){
?>
<table class="table">
<thead>
<tr>
<th>Originator</th>
<th>Collaborator</th>
<th>engagement_partner</th>
<th>Niche</th>
<th>Fee Potential</th>
</tr>
</thead>
<tbody>
<?php foreach($entries as $entry){ ?>
<tr>
<td><b> <?php echo $entry->originator; ?></b></td>
<td><?php echo $entry->collaborator; ?></td>
<td><?php echo $entry->engagement_partner; ?></td>
<td><?php echo $entry->niche; ?></td>
<td><?php echo $entry->fee_potential;?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php}?>
It doesn't looks like a GROUP BY statement is what you are looking for.
Seeing your example HTML structure, the thing you are looking for is an ordered list of rows. You can then use php to sort the results and then use them
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM `entries`");
$sortedRows = [];
foreach($rows as $row) {
if (!isset($sortedRows[$row->originator])) {
$sortedRows[$row->originator] = [];
}
$sortedRows[$row->originator][] = $row;
}
a different approach is using ORDER BY in the query, which i won't fully work out since i dont know what your approach is on creating the HTML structure:
global $wpdb;
$rows = $wpdb->get_results( "SELECT * FROM `entries` ORDER BY `originator` ASC");
$currentOriginator = NULL;
foreach ($rows as $row) {
if ($row->originator !== $currentOriginator) {
//create table
$currentOriginator = $row->originator;
}
//add the row to the table
}
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>
I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>