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.
Related
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
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!
I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway
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.
I'm trying to make a shopping cart and have managed to submit what checkboxes a user has checked to my shopping cart page. It echos the details from the database but I've realised that I cant get my details to post through on their own.
There is one big group, is there a way to add to my code to ungroup them and put them in a table?
this is the code ive used to post the deatils to my cart:
<?php
if (isset($_POST['games'])) {
$n = count($_POST['games']);
for($i=0; $i < $n; $i++)
echo $_POST['games'][$i];
}
?>
this is the database:
<?php
$con = pg_connect(bla bla);
if (!$con){
die('Could not connect: ' . pg_error());
}
$result = pg_query("SELECT * FROM CSGames");
echo "
<table>
<tr>
<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
<th>Select</th>
</tr>";
while($row = pg_fetch_array($result)){
echo"<tr>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo '<td><input type="checkbox" name="games[]" value="' . $row['0'] . $row['1'] . $row['2'] . $row['3'] . $row['4'] . '"/></td>';
echo"</tr>";
}
echo"</table>";
pg_close($con);
?>
The problem with the data you are getting from $_POST['games'] lies in how you are populating the 'value' property of the input.
While personally I would add a game ID to your database table and only put the value of the game's ID as the input's value and just look up the data for the selected games on your cart output page, I will stick with the framework within how you have done things for my answer.
This is your problem:
<input type="checkbox" name="games[]" value="' . $row['0'] . $row['1'] . $row['2'] . $row['3'] . $row['4'] . '"/>
Here you are just concatenating a bunch of strings together without any sort of separator that you can later split on. I would simply suggest doing something like this:
<input type="checkbox" name="games[]" value="' . $row['0'] . '|||' . $row['1'] . '|||' . $row['2'] . '|||'. $row['3'] . '|||' . $row['4'] . '"/>
I have added triple pipes ||| that you can later use to split these fields on.
So when you get to outputting, you can do something like this:
<?php
if (isset($_POST['games'])) {
?>
<table>
<tr>
<tr>
<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
</tr>
<?php
foreach($_POST['games'] as $game_string) {
$game = explode('|||', $game_string);
?>
<tr>
<?php
foreach ($game as $attr) {
?>
<td><?php echo $attr; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
<?php
}
?>