I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc
Related
I used to pass value from one page to another using session. E.g
page1.php
<?php
session_start();
$_SESSION["id"] = $id;
?>
page2.php
<?php
session_start();
echo $_SESSION["id"];
?>
But for the following case i don't know how can i pass the id to the next page. This code is used to display group of people and the action column have an option to view the people details.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<td>' . '<a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> </td>'
?>
</tr>
<?php } ?>
</tbody>
How can i replace <a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> to not showing ID in the URL?
Please try this with the hyper link it is not possible to hide the GET data.We need to use POST method.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<form name="test" method="post" action="testing.php">';
echo '<input type="hidden" name="hidden_name" value="'.$SystemAccessDB['id'].'"/>';
echo '<button type="submit">View</button>';
echo '</form>';
?>
</tr>
<?php } ?>
</tbody>
When you will click it will redirect to testing.php with hidden value 'hidden_name' on which you can further query according to that value.
I am having syntax error with my following code
<?php
If (!empty($_SESSION['LogedinStudentId'])) {
echo '<h3>Your Scholarship Applications:</h3>
<table width="100%" class="table table-bordered">
<tr>
<th scope="col">Sr.No.</th>
<th scope="col">Date of Application</th>
<th scope="col">Course Type</th>
<th scope="col">Course Description</th>
<th scope="col">Subject</th>
<th scope="col">Applied for Semester No.</th>
<th scope="col">Scholarship Status</th>
<th scope="col">View / Print</th>
</tr>
<tr>
<td>' . ++$serialno . '</td>
<td>' . if(empty($row_studentdashboard['DateofApplication'])) {
echo ' ';
} else {
echo date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
. '</td>
<td>' . $row_studentdashboard['CourseType'] .'</td>
<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>
<td>' . $row_studentdashboard['Subject'] .'</td>
<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>
<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>
<td>View / Print</td>
</tr>
</table>';
} else {
echo '<h3>You do not have any application pending</h4>';
}
?>
I am getting syntax error on line no. 17 and 22. The second (nested) if statement is throwing syntax error. I can not judge what is wrong. If I run this second if statement outside of the html it is working fine.
Can anyone point out what's wrong?
What you are doing is an if-statement inside of echo-statement. It is wrong.
Run second if-statement outside of html and create a variable that you later print in your html.
A kind of this:
if(empty($row_studentdashboard['DateofApplication'])) {
$text = ' ';
} else {
$text = date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
}
.....
<td>' . ++$serialno . '</td>
<td>' . $text . '</td>
You 're not supposed to concatenate an if statement to a string. That is what you did on line 17/18
Try :
<?php
if (isset($_SESSION['LogedinStudentId']) && !empty($_SESSION['LogedinStudentId'])) {
$out = '<h3>Your Scholarship Applications:</h3>';
$out .= '<table width="100%" class="table table-bordered">';
$out .= '<tr>';
$out .= '<th scope="col">Sr.No.</th>';
$out .= '<th scope="col">Date of Application</th>';
$out .= '<th scope="col">Course Type</th>';
$out .= '<th scope="col">Course Description</th>';
$out .= '<th scope="col">Subject</th>';
$out .= '<th scope="col">Applied for Semester No.</th>';
$out .= '<th scope="col">Scholarship Status</th>';
$out .= '<th scope="col">View / Print</th>';
$out .= '</tr>';
$out .= '<tr>';
$out .= '<td>' . ++$serialno . '</td>';
$out .= '<td>';
if(!isset($row_studentdashboard['DateofApplication']) || empty($row_studentdashboard['DateofApplication'])) {
$out .= ' ';
} else {
$out .= date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
$out .= '</td>';
$out .= '<td>' . $row_studentdashboard['CourseType'] .'</td>';
$out .= '<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>';
$out .= '<td>' . $row_studentdashboard['Subject'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>';
$out .= '<td>View / Print</td>';
$out .= '</tr>';
$out .= '</table>';
} else {
$out = '<h3>You do not have any application pending</h4>';
}
echo $out;
I have a query that selects all of the users in a database that are not approved.
$query = "SELECT * FROM users WHERE approved = '0';
$data = mysqli_query($dbc, $query);
Then, what I want to do is display that list of people in a table that has a column with a checkbox to approve that person.
<table class="table mb-none">
<thead>
<tr>
<th>Picture</th>
<th>Username</th>
<th>Profile Type</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Approve</th>
<th>Deny/Delete</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($data)){
echo '<tr>'
echo '<td><img width="70px" height:"90px" src="' . EHS_UPLOADPATH_STUDENTS . $row['picture'] . '"/></td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>Student</td>';
echo '<td>' . $row['first_name'] . '</td>';
echo '<td>' . $row['last_name'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['phone_number'] . '</td>';
echo '<td style="text-align:center; padding:20px; background-color:#DFF0D8;">';
echo '<input type="checkbox" name="approve[]" value = "'.$row['user_id'].'"></td>';
echo '<td style="text-align:center; padding:20px; background-color:#FCDEDE;"><button type="button" class="mb-xs mt-xs mr-xs btn btn-danger">Deny/Delete</button> </td>';
echo '</tr>';}
This is how I am trying to process this:
if(isset($_POST['submit'])){
if(!empty($_POST['approve'])){
$users = $_POST['approve'];
foreach($users as $user){
$query = "UPDATE users SET approved = '1' WHERE user_id = '". $user . "'";
mysqli_query($dbc, $query);}}} ** here goes the rest of the code
Now, the problem I am having is that when I click on the "approve" checkboxes and hit "Submit" only the first item in the array approve[] is being processed and not the rest. I have no idea why. I have been thinking about this for the last 2 hours and can't figure it out.
try the following approach :
if(isset($_POST['submit'])){
if(!empty($_POST['approve'])){
foreach($_POST['approve'] as $user){
$query = "UPDATE users SET approved = '1' WHERE user_id = '". $user . "'";
mysqli_query($dbc, $query);}}} ** here goes the rest of the code
If that does not work ,do a print_r($_POST['approve']) and see what the contents of the array are.
I found the solution... I put the closing bracket for the "foreach" in the wrong place. Thanks for your help!
Is there a better solution, apart from a template engine like smarty, for huge code like this?
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<?php
$output = '<th class="sorting_numeric_html sorting_default_desc">' . TH_ORDERS_ID . '</th>';
$output .= '<th class="sorting_date_eu">' . TH_ORDERS_DATE . '</th>';
$output .= '<th>' . TH_ORDERS_NAME . '</th>';
$output .= '<th>' . TH_ORDERS_STATUS . '</th>';
$output .= '<th class="sorting_disabled">' . TH_ACTION . '</th>';
echo $output;
?>
</tr>
</thead>
<tbody>
<?php
$output = '';
foreach ($orders['RESULT'] as $order) {
$output .= '<tr>';
$output .= '<td class="text-right">' . inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']) . '</td>';
$output .= '<td>' . inc_datetime_short($order['date_purchased']) . '</td>';
$output .= '<td>' . inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id']. '&action=edit'), $order['delivery_name']) . '</td>';
$output .= '<td class="status' . $order['orders_status'] . '">' . $order['orders_status_name'] . '</td>';
$output .= '<td class="btn-group actions">';
$output .= inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"');
$output .= modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"');
$output .= '</td>';
$output .= '</tr>';
}
echo $output;
?>
</tbody>
</table>
For sure Smarty would be a solution, but is there another way?
I'd do it like this, makes it easier to read as the HTML is a little bit more separated from the PHP:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc"><?php echo TH_ORDERS_ID; ?></th>
<th class="sorting_date_eu"><?php echo TH_ORDERS_DATE; ?></th>
<th><?php echo TH_ORDERS_NAME; ?></th>
<th><?php echo TH_ORDERS_STATUS; ?></th>
<th class="sorting_disabled"><?php echo TH_ACTION; ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($orders['RESULT'] as $order): ?>
<tr>
<td class="text-right"><?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']); ?></td>
<td><?php echo inc_datetime_short($order['date_purchased']); ?></td>
<td><?php echo inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id'] . '&action=edit'), $order['delivery_name']); ?></td>
<td class="status<?php echo $order['orders_status']; ?>"><?php echo $order['orders_status_name']; ?></td>
<td class="btn-group actions">
<?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"'),
modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"'); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Instead of <php echo you can also use <?=, but lots of people dislike the use of it. It's only 4 more characters to put echo so i'd stick with that!
Yes, you normally want to separate the most you can PHP and HTML, leaving the html alone, like this:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc">
<?php echo TH_ORDERS_ID; ?>
</th>
<th class="sorting_date_eu">
<?php echo TH_ORDERS_DATE; ?>
</th>
...
If your system supports it, you can try instead changing the <?php echo for <?=, which is known as short tags as read in the manual, in which case the code would look much neater:
<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
<th class="sorting_numeric_html sorting_default_desc">
<?= TH_ORDERS_ID; ?>
</th>
<th class="sorting_date_eu">
<?= TH_ORDERS_DATE; ?>
</th>
...
However, a couple of notes that would require you to change your code further down. They might not be for now, but they more for your future code:
Why are you echoing constants? Normally you'd like to store your data in variables and then echo them.
It's better normally to avoid putting too many classes in the html since it's the CSS what gets cached better normally. I'd go with an id="tableorders" and then simply class="id", class="date" and so on.
Currently, each td../td prints in just 1 line which makes the source code very hard to read, any way I can print each td../td element on new line to make it much more readable.
thanks for replies. i tried the break but it prints on screen, need to put it in here but where?
$entries[$i] = '<td>'.$id[$i] .'</td>';
$entries[$i] .= '<td>'.$username[$i].'</td>';
$entries[$i] .= '<td>'.$first_name[$i].'</td>';
Further Edit:
The relevant HTML
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<div class="row">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Country</th>
<th>Referrer</th>
</tr>
</thead>
<tbody>
<tr>
<?php
if (isset($content) && !empty($content) && is_array($content)) {//2
$i = 0;
$entries = array();
while(array_key_exists($i, $content)) {//1
$entries[$i] = "\n" .'<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" .'<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$first_name[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$last_name[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$email_address[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$country[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$where_about[$i].'</td>';
//$entries[$i] .= '<td>'.$sample[$i].'</td>';
echo $entries[$i];
$i++;
}//1
}//2
?>
</tr>
</tbody>
</table>
concat "\n" to the front of the statement
Edit
$entries[$i] = "\n" . '<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" . '<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" . '<td>'.$first_name[$i].'</td>';
A neat trick is to define a constant with a linebreak:
define('NL', "\r\n");
Then we just concat that constant not worrying about using single our double qoutes usage:
$entries[$i] = '<td>'.$id[$i] .'</td>' . NL .
'<td>'.$username[$i].'</td>' . NL .
'<td>'.$first_name[$i].'</td>' . NL;
Same trick ofcourse can be applied to tabs:
define('TAB', "\t");
So we can do something radical like this, for instance:
$entries[$i] = '<tr>' . NL .
TAB . '<td>' . $id[$i] . '</td>' . NL .
TAB . '<td>' . $username[$i] . '</td>' . NL .
TAB . '<td>' . $first_name[$i] . '</td>' . NL .
'</tr>' . NL;
Spaces are up to personal preference ofcourse but when you're consistent it even keeps your code readable!
And in your case you could make your code even cooler by working through those columns (<td>) with a foreach loop through all fields.
$entries[$i] = '<tr>' . NL;
foreach(array('id', 'username', 'first_name') as $key) {
$entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}
$entries[$i] = '</tr>' . NL;
Now tell me that isn't cool!
Seems like your code simply lacks the intelligence to add each column in its own table row (tr), here is your relevant PHP code with the fixes and suggested additions, it should work but I can't test it.
<?php
define('NL', "\r\n");
define('TAB', "\t");
if (isset($content) && !empty($content) && is_array($content)) {
$i = 0;
$entries = array();
$fields = array('id', 'username', 'first_name', 'last_name', 'email_address', 'country', 'where_about');
while(array_key_exists($i, $content)) {
$entries[$i] = '<tr>' . NL;
foreach($fields as $key) {
$entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}
$entries[$i] = '</tr>' . NL;
echo $entries[$i];
$i++;
}
}
?>
As I am reading this, walking through $content with a while might not be good idea, try foreach($content as $index => $value) instead then would make $index the equivelant of $i but you wouldn't need to use $content[$index] because that exact value will be present in $value in a foreach.
Use '\n' after the td tag you echo/print
You can add a line break to your print statement.
echo "My text\n";
You can also add tabs to get it indented neatly.
echo "\t\tMy text\n";
<?
foreach($array as $single) {
echo "<td></td> \n";
}
?>
I would just like to add: If you're using Windows to read the code, you should use \r\n instead of just \n
Not sure what you're asking but if you want to get rid of the :
$s .= '<td>' . $n . '</td>';
use ($co means close open)
$co = '</td><td>';
$row = '<tr><td>' . $data . $co . $data2 . $co . $data3 . '</td></tr>';