Issue with printing data to a table from a database - php

i would like to to display a table but all I've got is a message that contains: Resource id #5,
my session.php contains my db connection and some user information, I've used it at other pages so i don't think it's the cause.
i'm a not a pro with the web :3 .
here is my code:
<?php
include('session.php');
$username=$_SESSION['username'];
$result = pg_query($db,"SELECT * FROM public.antennas WHERE idop IN (SELECT idop FROM public.user WHERE username_u='$username' ) ");
return $result;
$output = '';
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id Antennas</th>
</tr>';
if(pg_num_rows($result) > 0)
{
while($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
$output .= '
<tr>
<td>'.$row["id_a"].'</td>
<td class="id_a" data-id1="'.$row["id_a"].'" contenteditable>'.$row["id_a"].'</td>
<td><button type="button" name="delete_btn" data-id7="'.$row["id_a"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="id_a" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
echo 'success';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
echo 'problem';
}
$output .= '</table>
</div>';
echo $output;
?>

You have return $result; on Line 5.
Remove it, you're forcing the PHP script to exit therefore everything after the return is not executed

Related

How do I display image in the right column in an sql table?

I am inserting values to an sql table and displaying it through AJAX. The values get displayed properly but the image does not come in the right column, instead it comes on top. Why does this happen?
Although the image is shown in the webpage, it is however aligned in different position.
<?php
...
...
...
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Time</th>
<th>Img</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["Name"].'</td>
<td>'.$row["Time"].'</td>
<td>' ?> <img src="<?php echo $row['Img']; ?>" alt="my picture" height="100" width="100" /> <?php '</td> //Here is the problem where I am trying to display the image
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Time</th>
<th>Img</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["Name"].'</td>
<td>'.$row["Time"].'</td>
<td> <img src="' .$row["Img"] . '" alt="my picture" height="100" width="100" /> </td> //Here is the problem where I am trying to display the image
</tr>';
}
$output .= '</table></div>';
echo $output;
}
else
{
echo 'Data Not Found';
}
try this way.

Warning: Invalid argument in php

This file has an error invalid argument supplied for foreach. Does anyone solve this issue. I tried lot of time but i could not find it,I don't know how to solve this. Here is My code:
<?php
include('connect.php');
$query = "SELECT * FROM product";
$result = mysqli_query($connect,$query);
$output ='<table class="table table-stripped table-bordered">
<tr><th>Product Name</th>
<th>Product Type</th>
<th>Product Rate</th>
<th>Brand</th>
<th>Edit</th>
<th>Insert</th>
</tr>';
if(mysqli_num_rows($result)>0)
{
foreach (mysqli_num_rows($result) as $row) {
$output .= '<tr>
<td width="40%">'.$row["product_name"].'</td>
<td width="40%">'.$row["product_type"].'</td>
<td width="40%">'.$row["product_rate"].'</td>
<td width="40%">'.$row["brand"].'</td>
<td width="10%">
<button type="button" name="edit" class="btn btn-primary btn-xs edit" id="'.$row["id"].'">Edit</button>
</td>
<td width="10%">
<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'">Delete</button>
</td>
</tr>';
}
}
else
{
$output .='<tr>
<td colspan ="4" align="center"> No data Found</td>
</tr>' ;
}
$output .= '</table>';
echo $output;
?>
mysqli_num_rows function returns a number, not an array. You probably want to use mysqli_fetch_all:
foreach (mysqli_fetch_all($result) as $row) {
}
The mistake you made is that you use the num_rows function in the foreach. I always prefer to use a while-loop with a fetch.
while ($row = mysqli_fetch_assoc($result)) {
$output .= '<tr>
<td width="40%">'.$row["product_name"].'</td>
<td width="40%">'.$row["product_type"].'</td>
<td width="40%">'.$row["product_rate"].'</td>
<td width="40%">'.$row["brand"].'</td>
<td width="10%">
<button type="button" name="edit" class="btn btn-primary btn-xs edit" id="'.$row["id"].'">Edit</button>
</td>
<td width="10%">
<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'">Delete</button>
</td>
</tr>';
}
mysqli_fetch_array is the solution to your problem. foreach requires an array to iterate it and the earlier method provides you the same.
mysqli_fetch_assoc will return row as associative array, if you want complete rows as associative array you must use mysqli_fetch_all($res, MYSQLI_ASSOC);

If I delete all users in my table, the option to add users disappears as well. #STUMPED

PHP - AJAX, and MySqli
I have a small application that allows me to add or delete a first and last name to a database within the webpage. I am using AJAX so the page updates instantly w/out refreshing. My issue is that if I delete all the users from the page, the ability to add user also disappears. Consequently, this is triggering my "if, else" statement to fire and I am left with a user table that reads simply, "Data not found". Any help will be GREATLY appreciated.
<?php
$connect = mysqli_connect("", "", "", "");
$output = '';
$sql = "SELECT * FROM my_table ORDER BY id ASC";
$result = mysqli_query($connect, $sql);
$output .= '
<div id="table">
<table class="table table-bordered">
<tr>
<th width="40%">First Name</th>
<th width="40%">Last Name</th>
<th width="20%">Delete</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td class="first_name" data-id1="'.$row["id"].'">'.$row["first_name"].'</td>
<td class="last_name" data-id2="'.$row["id"].'">'.$row["last_name"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="first_name" contenteditable></td>
<td id="last_name" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
If I delete all users in my table, the option to add users disappears as well.
Sure, because the row with add user button...
$output .= '
<tr>
<td></td>
<td id="first_name" contenteditable></td>
<td id="last_name" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
Is inside this
if(mysqli_num_rows($result) > 0)
{
// [...]
}
If you get no rows (users) the html table row with the button is not appended to output.
Move the whole block at the end, just before
$output .= '</table>
</div>';
outside of the if block

PHP - while inside while in table

I have a table that I want to assign a staff member to for each row. I want a dropdown on the last column with all the staff members so I can assign a staff member by clicking their username in the dropdown.
I'm trying to do a while loop inside a while loop but am getting an error.
This is my current code:
<table class="table table-bordered table-striped js-dataTable-full table-header-bg">
<thead>
<tr>
<th>Actions</th>
<th>Username</th>
<th>Service</th>
<th>Price</th>
<th>Date Ordered</th>
<th>Account Email</th>
<th>Account Password</th>
<th>Status</th>
<th>Assign to staff</th>
</tr>
</thead>
<tbody>
<?php
$clients_result = mysqli_query($con, "SELECT * FROM boosting_orders ORDER BY id ASC");
$query = mysqli_query( $con, "SELECT * FROM users" );
if(mysqli_num_rows($clients_result) > 0) {
while($payment_row = mysqli_fetch_array($clients_result)) {
echo '
<tr id="no_enter">
<td style="text-align: center;">
<div class="btn-group">
<a class="btn btn-xs btn-default" type="button" data-toggle="tooltip" title="" data-original-title="Edit Order" href="orders_admin?action=edit&identification='.$payment_row['id'].'"><i class="fa fa-pencil"></i></a>
<a class="btn btn-xs btn-default" type="button" data-toggle="tooltip" title="" data-original-title="Remove Order" href="orders_admin?action=delete&identification='.$payment_row['id'].'"><i class="fa fa-times"></i></a>
</div>
</td>
<td>
'.$payment_row['users_name'].'
</td>
<td>
'.$payment_row['service'].'
</td>
<td>
$'.$payment_row['price'].'
</td>
<td>
'.$payment_row['date_ordered'].'
</td>
<td>
'.$payment_row['email'].'
</td>
<td>
'.$payment_row['password'].'
</td>
<td>
'.($payment_row['status'] == 'Completed' ? '<span class="label label-success" data-toggle="tooltip" title="" data-original-title="Your account has successfully been boosted">Completed</span>' : '<span class="label label-info" data-toggle="tooltip" title="" data-original-title="This order is either still pending or corrupt">Pending</span>').'
</td>
<td>
<select name="assign_staff" class="form-control">
'. while($row = mysqli_fetch_array($query)) { .'
<option value="'.$row['username'].'">'.$row['username'].'</option>
'. } .'
</select>
</td>
</tr>
';
} } else { } ?>
</tbody>
you can't concatenate string with while syntax,
the code in the last <td> should be:
echo '<select name="assign_staff" class="form-control">';
while ($row = mysqli_fetch_array($query)) {
echo '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
}
echo '</select>

Show custom titles on 2 <tr> in loop

I have displaying 10 results from database in table on the page. Last 2 results eg. 9th and 10th rows are with custom titles on the table. The code for showing them
if($result_rates->num_rows>0){
$counter = 1;
foreach ($result_rates as $r){
if($counter <9 )
{
echo '<tr>
<td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>
<td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
else {
echo '<tr>
<td id="people-'.$r['number_of_people'].'">Number '.$counter.'</td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
$counter++;
}
}
The result which is displayed on page is this
Is it possible to put on last two rows Number 9 and Number 10 different titles? The goal is both last rows be with custom 's
I've tried to add 2 <tr>'s in the else block but they've got doubled.
Try this:
if($result_rates->num_rows>0){
$counter = 1;
foreach ($result_rates as $r){
if($counter <9 )
{
echo '<tr>
<td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>
<td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
elseif($counter == 9){
echo '<tr>
<td id="people-'.$r['number_of_people'].'">Number 9 '.$counter.'</td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
elseif($counter == 10){
echo '<tr>
<td id="people-'.$r['number_of_people'].'">Number 10 '.$counter.'</td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
$counter++;
}
}
You can also try this if you want to minimise the code:
if($result_rates->num_rows>0){
$counter = 1;
foreach ($result_rates as $r){
if($counter <9 )
{
echo '<tr>
<td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>
<td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
else{
if($counter == 9){
$custom_title= "number 9";
}elseif($counter == 10){
$custom_title= "number 10";
}
echo '<tr>
<td id="people-'.$r['number_of_people'].'">'.$custom_title.' '.$counter.'</td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
$counter++;
}
}
You can write like this sort way so you don't need to write same code again.
if ($result_rates->num_rows > 0) {
$counter = 1;
foreach ($result_rates as $r) {
if ($counter < 9) {
$custom_titles = $r['number_of_people'];
} else {
$custom_titles = "Number " . $counter;
}
echo '<tr>
<td id="people-' . $r['number_of_people'] . '">' . $custom_titles . '</td>
<td>
<td>
<button id="' . $r['rate_id'] . '" onclick="edit_row(' . $r['rate_id'] . ')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
$counter++;
}
}
From your question and your comment I think you want to get last two rows dynamically(not fix 9 & 10)
$total_records = $result_rates->num_rows; //count number of rows
if($result_rates->num_rows>0){
$counter = 1;
foreach ($result_rates as $r){
if($counter <($total_records-2) ) //(rows - 2) i.e. before last two rows
{
echo '<tr>
<td id="people-'.$r['number_of_people'].'">'.$r['number_of_people'].'</td>
<td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
else {
echo '<tr>
<td id="people-'.$r['number_of_people'].'">Number '.$counter.'</td>
<td>
<button id="'.$r['rate_id'].'" onclick="edit_row('.$r['rate_id'].')" class="btn btn-primary edit_button">Edit</button>
</td>
</tr>';
}
$counter++;
}
}

Categories