No result on table - php

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>';

Related

Echoing html if data from SQL if statement true

So I've been struggling at how to echo this set of HTML with users that are set as admin from my database. I've looked at quite a few places for information but I'm struggling to get it to work. Perhaps I'm doing something really stupid. Thanks for your help.
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
?>
I think that you forgot to put the while between {}. Here is the correct code:
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
}
?>

Adding an active class according to URL

I have a php/mysql driven menu. I am trying to add an active class to the menu item whenever the URI reflects that menu item (basically, when I am on that page) but every example I find is with a menu that is not populated through an array or repeater like mine.
How can manipulate the active class you see below so that it shows only when I am on that page?
Here is my menu
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['id'] == 2){
// below is the active class that is now being populated accross all items equally
echo '<li class="nav-item dropdown active">';
echo '<a href="#" class="nav-link dropdown-toggle animated fadeIn animation-delay-7" data-toggle="dropdown" data-hover="dropdown" role="button" aria-haspopup="true" aria-expanded="false" data-name="services">';
echo $row["pagename"];
echo '<i class="zmdi zmdi-chevron-down"></i>';
echo '</a>';
// create a new list
echo '<ul class="dropdown-menu">';
// loop second/submenu query results
while($sub_row = $result2->fetch_assoc()) {
echo '<li>';
echo '' . $sub_row["pagename"] . '';
echo '</li>';
}
echo "</ul>";
echo '</li>';
} else { // for all other menu links
echo '<li class="nav-item dropdown active">';
echo '' . $row["pagename"] . '';
echo '</li>';
}
}
} else {
echo "0 results";
}
?>
The screenshot below shows the result after it renders. Notice the active class highlighted in yellow, it is added to every item in my main menu (the way I have it coded, which it's wrong of course). The green is the $sub_row["link"] (to answer Mark's question below
Notice, the active class is only needed to be added programmatically on the main menu. (the else portion of my code).
The screenshot below shows how the active class affects the menu item if the user was in the www.domain.com/how-it-works.php page. The active class contians the styles to make the menu item appear that way.
Thanks in advance
Hard to test with not having the DB outputs to match against mate but try this, basically you want an if statement to get the current page URL and match it against the url you get back from the DB.
<?php
if ($result->num_rows > 0) {
$pageURL = str_replace('/', '', $_SERVER['REQUEST_URI']);
$homeURL = $_SERVER['REQUEST_URI'];
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['id'] == 2){ ?>
<li class="nav-item dropdown">
<a href="" class="nav-link dropdown-toggle animated fadeIn animation-delay-7" data-toggle="dropdown" data-hover="dropdown" role="button" aria-haspopup="true" aria-expanded="false" data-name="services">
<?php echo $row["pagename"];?>
<i class="zmdi zmdi-chevron-down"></i>
</a>
<ul class="dropdown-menu">
<?php while($sub_row = $result2->fetch_assoc()) {?>
<li>
<a href="<?php echo $sub_row["link"]; ?>" class="dropdown-item">
<?php echo $sub_row["pagename"] ?>
</a>
</li>
<?php } ?>
</ul>
</li>
<?php
} else if($row['id'] == 1){ ?>
<li class="nav-item dropdown <?php echo ($homeURL == $row["link"])? 'active' : '';?>">
<a href="<?php echo $row["link"]; ?>" class="nav-link dropdown-toggle animated fadeIn animation-delay-7">
<?php echo $row["pagename"]; ?>
</a>
</li>
<?php
} else { ?>
<li class="nav-item dropdown <?php echo ($pageURL == $row["link"])? 'active' : '';?>">
<a href="<?php echo $row["link"]; ?>" class="nav-link dropdown-toggle animated fadeIn animation-delay-7">
<?php echo $row["pagename"]; ?>
</a>
</li>
<?php }
}
} else {
echo "0 results";
}
?>
Edit:
Bit that gets the current page url is:
$pageURL = end(explode('/', $_SERVER['REQUEST_URI']));
Bit that does the check is (this is just a shorthand if statement:
<?php echo ($pageURL == $row["link"])? 'active' : '';?>

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;">';

Multi-dimensional arrays, but I cannot retrieve data 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; ?>

php array and returning a default value?

Here is my code:
<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<?php
$currencies = explode(',', hana_get_opt('_cc'));
foreach ($currencies as $currency) {
if ($currency == get_option('woocommerce_currency')){
echo $currency;
break;
}else{
echo "Select Currency";
break;
}
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
<?php
foreach ($currencies as $currency) {
if ($currency == get_option('woocommerce_currency'))
echo '<li>' . $currency . '</li>';
else
echo '<li>' . $currency . '</li>';
}
?>
</ul>
</div>
</div>
Which works great it returns a list of my currencies and updates the page accordingly, I have just on question if the user has yet to set a value I would like it to say either select currency or just apply the first option from my hana_get_opt('_cc') array as the default.
Here is the html generated code:
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">undefined<span class="caret"></span></a>
<ul class="dropdown-menu currency_switcher">
<li>SEK</li>
<li>EUR</li>
</ul>
</div>
I am no php coder and much appreciate any help provided
Chris
if(get_option('woocommerce_currency')==' ')
{
$currencies = explode(',', hana_get_opt('_cc'));
foreach ($currencies as $currency)
{
if $currency == get_option('woocommerce_currency'){
{
echo $currency;
break;
}
else{
echo "Select Currency";
break;
}
}
}
else
{
echo "Select Currency";
$currencies = explode(',', hana_get_opt('_cc'));
$currency=$currencies [0];
}
based on the assumption that get_option('woocommerce_currency') would return null if no currency is selected
<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<?php
$currencies = explode(',', hana_get_opt('_cc'));
$currentCurrency = get_option('woocommerce_currency')
if ($currentCurrency === false) {
echo "select currency";
}
else{
echo $currentCurrency;
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
<?php
foreach ($currencies as $currency) {
if ($currency == $currentCurrency)
echo '<li>' . $currency . '</li>';
else
echo '<li>' . $currency . '</li>';
}
?>
</ul>
replace the if clause if ( $currentCurrency != null ) with whatever applies, if get_option('woocommerce_currency') returns anything other as null if not yet set

Categories