Multi-dimensional arrays, but I cannot retrieve data PHP - php

More errors from my most recent project (posted about another thing earlier).
I've been struggling with this error for a few days now. PHP is not my main programming language, nor am I that great at it.
I'm aware that mysql_* should not be used, also.
Anyways. Here's the code snip.
<?php
$query = "SELECT * FROM events";
$result = mysql_query($query);
$content = array();
$num = mysql_num_rows($result);
if ($num > 0) {
while($row = mysql_fetch_assoc($result)) {
$content[$row['ID']] = $row;
}
}
if ($num > 0) {
?>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($content)));?></th><th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($content as $tablerow): ?>
<tr>
<td><?php echo implode('</td><td>', $tablerow);?>
</td><td><div class="btn-group" role="group" aria-label="Actions"><a class="btn btn-primary" role="button" href="#">New Session</a><a class="btn btn-success" role="button" href="#">Continue Latest</a><a class="btn btn-warning" role="button" href="#">Settings</a><?php echo "<a class='btn btn-danger' role='button' href='/manager/delete?id=".$row."'>Delete</a>"; ?>
</tr>
<?php endforeach; ?>
</tbody>
Sorry for derp formatting. Anyways.
I'm trying to get that delete button to link to ../manager/delete?id=x, where x is the ID for the row. My database is formatted in the way where the ID is in a column labeled "ID". I'm trying to reference each of these rows by this ID... why is this not working?
I've also tried (in the place of row), content[row['ID']], content['ID'], 'row['ID']`, with different case on the ID (uppercase/lowercase).

Fred -ii- was correct in his above comment.
I had tried everything other than $tablerow['ID'].... now you know I guess.
Thank you for the help everyone. One of the dumbest things I've ever missed while debugging....

he was partially correct, however i think your id is the key, so you might just reference the key so its a bit cleaner
<?php foreach ($content as $idKey => $tablerow): ?>
<tr>
<td><?php echo implode('</td><td>', $tablerow);?>
</td><td><div class="btn-group" role="group" aria-label="Actions"><a class="btn btn-primary" role="button" href="#">New Session</a><a class="btn btn-success" role="button" href="#">Continue Latest</a><a class="btn btn-warning" role="button" href="#">Settings</a><?php echo "<a class='btn btn-danger' role='button' href='/manager/delete?id=".$idKey."'>Delete</a>"; ?>
</tr>
<?php endforeach; ?>

Related

Running PHP inside of PHP?

I am still learning and I found that I can't run PHP within PHP, after reading into it makes sense why you cannot do it (D'oh!), what I am struggling to find is the correct way to do this.
I have two buttons which link to different modals, if I do not use them within a IF Statement they work as expected as soon as I add to the IF Statement it breaks the page. (obviously because I am trying to run php within php)
What I'm trying to get working is to show a different button depending on the result of a column called "status" in the MySQL table, if it equals 1 it will show the edit button if anything else it will show a check-out button. I need to pass the <?php echo $fetch['id']?> to the data-target I'm not sure how to go about that.
<?php
$status_code = $fetch["status"];
if("$status_code" == "1")
{ echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<?
php echo $fetch['id']?>"><span class="glyphicon glyphicon-plus"></span>edit</button>';
} else
{ echo '<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal<?
php echo $fetch['id']?>"></span>Check-Out</button>';
} ?>
Any help is much appreciated.
You just need simple concatenation with the . (dot) operator.
E.g.
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal'.$fetch['id'].'"><span class="glyphicon glyphicon-plus"></span>edit</button>';
...etc.
This is used to join any two string values (whether hard-coded literals or variables) together. What's happening here is your code is building a string from several components and then echoing it.
Documentation: https://www.php.net/manual/en/language.operators.string.php
You don't need to run PHP inside PHP.
You may use a comma (or a dot):
<?php
if ($status_code == "1") {
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<'
, $fetch['id']
, '"><span class="glyphicon glyphicon-plus"></span>edit</button>';
} else {
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal'
, $fetch['id']
, '">Check-Out</button>';
}
Or using short tags:
<?php if ($status_code === '1') : ?>
<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<?= $fetch['id'] ?>">
<span class="glyphicon glyphicon-plus"></span>edit</button>'
<?php else: ?>
<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal<?= $fetch['id'] ?>">Check-Out</button>'
<?php endif; ?>
You can have conditions (and other expressions) in short tags:
<button
type="button"
class="button-7"
data-toggle="modal"
data-target="#<?=
$status_code === '1'
? 'update_modal'
: 'checkout_modal'
?><?= $fetch['id'] ?>">
<?php if ($status_code === '1') : ?>
<span class="glyphicon glyphicon-plus"></span>edit
<?php else: ?>
Check-Out
<?php endif; ?>
</button>
And concatenate strings with dots:
<?php
$dataTargetPrefix =
$status_code === '1'
? 'update_modal'
: 'checkout_modal';
?>
<button
type="button"
class="button-7"
data-toggle="modal"
data-target="#<?= $dataTargetPrefix . $fetch['id'] ?>">
<?php if ($status_code === '1') : ?>
<span class="glyphicon glyphicon-plus"></span>edit
<?php else: ?>
Check-Out
<?php endif; ?>
</button>

No result on table

I like to retrieve a mysql result and assign to a variable ($status)
and depending on the result I want to put an ok or a ko (in this case if $status are "Active" the result is an "ok)
Seems that the problem are when show the result into the td but can't see something else.
 
Anybody can help?
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$status = strtolower($row['register_status']);
$data .= '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['email'].'</td>
<td>'
if ($status == "Active") {
echo "OK";
}
else {
echo "KO";
}
'</td>
<td width="1%">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Actions
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li onclick="GetUserDetails('.$row['id'].')">Update</li>
<li onclick="DeleteUser('.$row['id'].')">Delete</li>
</ul>
</div>
</td>
</tr>';```
The idea is change in the future the echo for an image or a div content
Instead of using echo, which will output "OK" or "KO" before the rest of your HTML, try determining whether you need "OK"/"KO" before you create your HTML.
You could also add an image or div or whatever you want instead of "OK" or "KO".
Then just concatenate this $value to your string like you are doing with the rest of your td's
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$status = strtolower($row['register_status']);
if ($status == "active") {
$value = "<div>Other Content</div>";
}
else {
$value = "<img src='http://example.com/img.jpg'>";
}
$data .= '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['email'].'</td>
<td>'.$value.'</td>
<td width="1%">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Actions
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li onclick="GetUserDetails('.$row['id'].')">Update</li>
<li onclick="DeleteUser('.$row['id'].')">Delete</li>
</ul>
</div>
</td>
</tr>';
First thing ,You are converting the status into the lower case and try to compare with "Active". It always gives you the incorrect answer every-time.
Rather than using the echo, you can first take the value of the status into one variable and than try to set under the table. It will work for you.
if ($status == "active") {
$status = "OK";
}
else {
$status = "KO";
}
$data .= '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['email'].'</td>
<td>'.$status.'</td>
<td width="1%">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Actions
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li onclick="GetUserDetails('.$row['id'].')">Update</li>
<li onclick="DeleteUser('.$row['id'].')">Delete</li>
</ul>
</div>
</td>
</tr>';

Post the id on delete in codeigniter

I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.
Here is my view:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
There are so many ways to do this, the best most common one is to get it from the url like this:
$id = #end($this->uri->segment_array());
Or this:
$id = $this->uri->segment(3);
Or you can do this by passing it in a hidden input like this:
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
Or using ajax.
delete link with confimation
Delete
call delete method in js and then show confimation msg.
Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly
<td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>
In controller delete function will be like this
public function delete($id){
echo $id;
}

passing php variable in javascript function

I am new to php and javscript.I am working on some project of question and answer system.I want to send question id to javascript function.
here is my php code
<?php
if ($result->num_rows > 0)
{
$i = 1;
while($row = $result->fetch_assoc())
{
$p = $row["description"] ;
echo '<div onclick="myOverFunction(<?= $p ?>)" align="left" class="j" role="group" aria-label="..." > <button id = "btnl<?=$i?>" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
echo $i;
echo '</button>';
echo '<p class="question" style ="display:inline; ">';
echo $p;
echo '</p>';
echo '</div>';
}
}
?>
My javacript function is:
<script>
function myOverFunction(x) {
document.getElementById("demo").innerHTML = x;
document.getElementById("btnl"+x).style.backgroundColor = "#d9534f";
}
</script>
my code does not working ..please help me ...
Your problem is:
echo '<div onclick="myOverFunction(<?= $p ?>)"
Change it to:
echo '<div onclick="myOverFunction('. $p .')".
Same goes for the other occurrences.
You can't echo from inside an echo
Your final echo should look like this:
echo '<div onclick="myOverFunction(\''. $p .'\')" align="left" class="j" role="group" aria-label="..." > <button id = "btnl'.$i.'" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
Note: If $p is a string, it needs quotes. And quotes inside quotes need escaping
You are breaking this line by trying to re-open php tags within the echo:
echo '<div onclick="myOverFunction(<?= $p ?>)" align="left" class="j" role="group" aria-label="..." > <button id = "btnl<?=$i?>" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';
Try this:
echo '<div onclick="myOverFunction('.$p.')" align="left" class="j" role="group" aria-label="..." > <button id = "btnl'.$i.'" type="button" class="btn btn-default btn-xs btn-round" style="border-radius:10px;">';

PHP Condition Setting for Button [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For the first page, I want to set the column 'content control' to be button 'update or delete' because it already got data for the particular row. While for the second page, I want to set the column 'content control' to be button 'add or update or delete' as it don't have data along with the row yet. The problem I faced is how to set the first page to contain button 'update or delete' while for the second page to contain button 'add or update or delete'.
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("smart_train",$conn);
$result = mysql_query("SELECT * FROM station");
?>
<?php
$i=0;
while($row = mysql_fetch_array($result)) {
$i%2==0;
switch ($i) {
case "0":
$classname="1Row";
$goesToPage='add_station_update1.php';
break;
case "1":
$classname="2Row";
$goesToPage='add_station_update2.php';
break;
case "2":
$classname="3Row";
$goesToPage='add_station_update3.php';
break;
case "3":
$classname="4Row";
$goesToPage='add_station_update4.php';
break;
case "4":
$classname="5Row";
$goesToPage='add_station_update5.php';
break;
case "6":
$classname="6Row";
$goesToPage='add_station_update6.php';
break;
case "6":
$classname="7Row";
$goesToPage='add_station_update7.php';
break;
case "7":
$classname="8Row";
$goesToPage='add_station_update8.php';
break;
case "8":
$classname="9Row";
$goesToPage='add_station_update9.php';
break;
case "9":
$classname="10Row";
$goesToPage='add_station_update10.php';
break;
}
?>
<tr>
<td> <?php echo $row["id"];?> </td>
<td> <?php echo $row["thailand"];?> </td>
<td> <?php echo $row["malaysia"];?> </td>
<td>
<div class="btn-group" class="<?php if(isset($classname)) echo $classname;?>">
<a href="<?php echo $goesToPage; ?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
</a>
<a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
<a><button type="button" class="btn btn-warning">Delete</button></a>
<?php if(isset($message)) { echo $message; } ?>
</div>
<div class="btn-group" class="<?php if(isset($addclass)) echo $addclass;?>">
<a href="<?php echo $goesToPage; ?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Add</button>
</a>
<a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
<a href="<?php echo $goesToPage; ?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
</a>
<a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
<a><button type="button" class="btn btn-warning">Delete</button></a>
<?php if(isset($message)) { echo $message; } ?>
</div>
<?php
$i++;
}
?>
</td>
</tr>
Your code is unclear you are missing pagination display part ... but anyway with current placed code i can suggest you this simplified algorithm, with following optimizations
First of all you should create add_station.php and update_station.php files which are expect from GET $_GET['id'] pram and then display your add or edit form fir data based on that id, we are going to use such links in html add_station.php?id=.$ID instead writing many switch and creating many duplicated add_station_update1..10.php files
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("smart_train",$conn);
$result = mysql_query("SELECT * FROM station");
?>
<?php
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td> <?php echo $row["id"];?> </td>
<td> <?php echo $row["thailand"];?> </td>
<td> <?php echo $row["malaysia"];?> </td>
<td>
<div class="btn-group" >
<?php if(empty($row["thailand"]) && empty($row["malaysia"]) ){?>
<a href="add_station.php?id=<?php echo $row["id"];?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Add</button>
</a>
<?php }?>
<a href="update_station.php?id=<?php echo $row["id"];?>">
<button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
</a>
<a><button type="button" class="btn btn-warning">Delete</button></a>
<?php if(isset($message)) { echo $message; } ?>
</div>
</td>
</tr>
<?php } ?>
also my suggestion to you do not display or word between buttons it is anyway clear for user that he can press or on update or on delete ...
if something from above code unclear just ask

Categories