Warning: Invalid argument in php - 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);

Related

Codeigniter load view with data after button submit

I have a table of each request filed by the user with corresponding actions to it. When I click on the update button, it doesnt load the view after submitting it.
This is my html code:
<table id="dataTableRequestDrafts" class="table table-striped table-bordered data-table-drafts" style="width:100%">
<thead>
<tr>
<th style="display: none;">Request ID</th>
<th>Request Type</th>
<th>Date Requested</th>
<th>Date Last Updated</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($param_drafts as $drafts){?>
<tr>
<td class="id" style="display: none;"><?php echo $drafts['idx']?></td>
<td class="type"><?php echo $drafts['request_type']?></td>
<td><?php echo $drafts['date_requested']?></td>
<td><?php echo $drafts['date_updated']?></td>
<td class="text-warning"><?php echo $drafts['status']?></td>
<td align="center">
<form action="<?php echo base_url('dashboard/staff/request/update_request_view'); ?>" method="post">
<input type="text" name="request_id" value="<?php echo $drafts['idx']?>" style="display: none;">
<button type="button" class="btn waves-effect waves-light btn-primary view-button"><i class="fa fa-eye"></i> View</button> <button type="submit" class="btn waves-effect waves-light btn-info update-button"><i class="fa fa-edit"></i> Update</button> <button type="button" class="btn waves-effect waves-light btn-danger delete-button" data-url="<?php echo base_url('dashboard/staff/request/delete');?>"><i class="fa fa-trash-o"></i> Delete</button>
</form>
</td>
</td>
</tr>
<?php } ?>
</tbody>
</table>
This is my function in the controller:
public function update_request_view()
{
$data = new stdClass;
$data->param_menu = 'request';
$idx = $this->input->post('request_id');
$type = $this->staffrequisition_model->getRequestType($idx);
$typeLower = strtolower($string = str_replace(' ', '', $type));
$commonContents = $this->staffrequisition_model->selectItem(array('idx'=> $idx));
$uncommonContents = json_decode($commonContents->contents);
$data->param_request = array_merge((array) $commonContents, (array) $uncommonContents);
$this->load->view('dashboard/staff/update_'.$typeLower.'_view', $data);
}
The view is seen in the response but it doesnt load the view. I was gonna use ajax but it would be tedious to load the data of the request in each input in the view and I want to return the whole html page not just a div.
Can you show the response here.
sometimes the response can't handle by browser. so it can't appear

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>

How would i add a drop down menu to my jquery table which uses mysql?

So currently I am trying to figure out how I would add a dropdown menu to the code I've posted below. I've searched the internet but could not find a way that worked for me, I'm very new to coding so this is kind of difficult for me others may find this easy.
<table class="table table-bordered table-striped">
<thead> <!-- add class="thead-inverse" for a dark header -->
<tr>
<th>ID</th>
<th>USERNAME</th>
<th>Crime(s)</th>
<th>Active</th>
<th>EDIT</th>
</tr>
</thead>
<tfoot>
<tr>
</tr>
</div>
</th>
</tfoot>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td class="username" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td>
<td class="crime" data-id2="'.$row["id"].'" contenteditable>'.$row["crime"].'</td>
<td class="activated" data-id2="'.$row["id"].'" contenteditable>'.$row["activated"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">DELETE</button></td>
</tr>
';
}
$output .= '
<tr>
<td></td>
<td id="name" contenteditable></td>
<td id="crime" contenteditable></td>
<td id="activated" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">ADD ACCOUNT</button></td>
</tr>
';
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
Since you mentioned that you'd like to add the dropdown to the activated section of your table and that it should contain Yes, No and Being Verified, you can update that table column to be
<td class="activated" data-id2="'.$row["id"].'" contenteditable>
<select name="activated" id="activated">
<option value="Yes" '.(($row["activated"] == "Yes") ? 'selected="selected"':"").'>Yes</option>
<option value="No" '.(($row["activated"] == "No") ? 'selected="selected"':"").'>No</option>
<option value="Being Verified" '.(($row["activated"] == "Being Verified") ? 'selected="selected"':"").'>Being Verified</option>
</select>
</td>
This would allow the value from the db $row["activated"] get automatically selected in the dropdown.

Issue with printing data to a table from a database

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

Categories