The text in echo does not show. Why isn't it displayed?
<?php
while ($liste1 = mysqli_fetch_assoc($resultat8)){
if(isset($liste1['naissancesID'])){
echo '<div class="photos" align="center"><img alt="" src="changements/naissances/images/' . $liste1["naissancesPic"] . '" class="deces">
<span class="caption">' . $liste1["naissancesName"] . '<p class="texte-deces">' . $liste1["naissancesDate"] . '</p></span></div>';
}else{
echo "No value in database";
}
}
?>
If no rows are returned, the while loop will end immediately, so you'll never get to the if statement. And when you fetch a row successfully, the ID field will always be filled in, so isset() will never return FALSE.
You should test the number of rows before the loop.
if (mysqli_num_rows($resultat8) > 0) {
while ($liste1 = mysqli_fetch_assoc($resultat8)){
if(isset($liste1['naissancesID'])){
echo '<div class="photos" align="center"><img alt="" src="changements/naissances/images/' . $liste1["naissancesPic"] . '" class="deces">
<span class="caption">' . $liste1["naissancesName"] . '<p class="texte-deces">' . $liste1["naissancesDate"] . '</p></span></div>';
}
}
} else {
echo "No value in database";
}
Related
Basically it is an element which shows an image from the path on database. But I just can't make it work.
<img class="img-circle profile_img" id="blah" src=" <?php $query2=mysqli_query($conexao,"select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
while($row2=mysqli_fetch_array($query2)){
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--cover" id="blah" src="'.$row2['img_local'].'" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src=1.png;">';
} else {
echo 'Show a default image.'; // <img src="path_to_default_image" alt="Default_image"/>
}
} ?>" alt="Avatar" title="DEFINIDA" >
I have managed to work, the final code:
<?php
$query2=mysqli_query($conexao,"select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
while($row2=mysqli_fetch_array($query2)){
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--profile" src="'.$row2['img_local'].'" title="Clique para abrir seu perfil">';
} else {
echo '<img class="image--profile" src="images/user.png">';
}
}
?>
This php code generates a table with some information like: temperature, water health, ph level and other things ...
if (count($auxGateway->getMaquina()) > 0)
{
foreach($auxGateway->getMaquina() as $maq)
{
echo '<div class="cont-maq-uni">';//Cria a div para cada maquina
echo ' <table class="maq">';//Cria uma tabela
echo ' <tr>';
echo ' <td colspan="2">';
echo ' <h2 class="maq-head">'.$maq->getNome().'</h2>';
echo ' </td>';
echo ' </tr>';
foreach($maq->getVariavel() as $var)
{
echo ' <tr class="maq">';
echo ' <td class="maq">';
echo ' <p class="n_font maq">'.$var->getNome().'</p>';
echo ' </td>';
echo ' <td class="maq">';
echo ' <p class="n_font maq">'.(floatval($var->getValor()) * floatval($var->getMultiplicador()) + floatval($var->getSomador())).' '.$var->getUnidadeValor().'</p>';
echo ' </td>';
echo ' </tr>';
}
echo ' </table>';
echo '</div>';
}
}
so I need to create a code to show some images if that information is bigger or less. For example: if 'temperature' <30 then it will show the image (notgood.jpeg). The problem is: how do I name these values since they are inside the MYSQL database? How will I name each row in the table?
Thank you guys!
You can use if..else.. or ternary operators
echo '<img src="' . ($var->getTemperature() < 30 ? 'notgood.jpeg' : 'good.jpeg') . '">' # using ternary operators;
This will give, if $var->getTemperature() < 30 <img src="notgood.jpeg">
else <img src="good.jpeg">
I have been trying to set up a page that lists prices of items from a table in a database. Here is my code:
<?php
$querylist = mysql_query("SELECT item_name,image,price,added_by FROM values");
while($row = mysql_fetch_array($querylist))
{
echo '<div class="post rareitem" style="margin-right: 15px;float: left;">
<div class="rarename">
<strong>';
// Shows Item Name
echo $row['item_name'];
echo '</strong>
</div>';
// Shows Item Image
echo '<div class="rareimage" style="background-image: url(/app/tpl/skins/Mango/images/values/rares/';
echo $row['image'];
echo ');"></div>';
// Shows Item Price
echo '<div class="rarecontrols">
<div class="coinsbox"></div>
<span>
<b> <b>Credits: </b> </b> ';
echo $row['price'];
echo '</span>';
// Shows Who Added the Item
echo '<div class="addedbox"></div><b><b><span><font color="#c93734"><font color="#c93734">Added By: </font> </font>';
echo $row['added_by'];
echo '</span></b></b>
</div>
<div class="clear"></div>
</div>';
}
?>
There is another chunk of code (shown below) that I have based this off of, and it works perfectly fine. I can't seem to get this to work though. I believe it has something to do with the SQL, the syntax, or something. No matter what I do, it produces absolutely no results, yet the code below results exactly as planned. I know for a fact it is not a connection issue because the below code can be placed on the same exact page as the above one and it works fine, however the above does not.
<?php
$querylist = mysql_query("SELECT id,username,motto,country,look,rank,account_created,role,account_created,online,last_online FROM users WHERE rank='9' ORDER BY ID LIMIT 20");
while($row = mysql_fetch_array($querylist))
{
echo '
<div class="team">';
// Showing Avatar
echo '<div style="float: left; margin-top:-1px;height: 60px; width: 64px; background: url(http://www.habbo.nl/habbo-imaging/avatarimage?figure=';
echo $row['look'];echo "&action=wav&direction=3&head_direction=3&gesture=sml&size=m) no-repeat 0px -10px";
echo "\"/>";
echo "<img alt=\"badge\" src=\"/app/tpl/skins/habbo/images/icons/";
echo $row['online'];echo ".gif\"></div>";
// Flags
echo "<br/><img src=\"/app/tpl/skins/habbo/images/icons/flags/";
echo $row['country'];echo ".png";
echo '" style="float:right;" /> <b><uf>';
echo $row['username'];echo "</u></b>";
// Bans & Ticket Count
$Bans = mysql_query("SELECT * FROM `bans` WHERE `added_by` = '" . $row['username'] . "'");
$BanCount = mysql_num_rows($Bans);
$Tickets = mysql_query("SELECT * FROM `moderation_tickets` WHERE `moderator_id` = '" . $row['id'] . "'");
$TicketCount = mysql_num_rows($Tickets);
//Role
echo "<br/><gb>Role: </b><fi> ";
echo $row['role'];echo "";
echo "</i>";
// Echoing bans & Tickets
echo "<br/><gb>Bans: </b><fi> ";
; echo $BanCount;
echo "</i>";
echo " <gb>Tickets: </b><if>";
; echo $TicketCount;
echo "</i>";
echo "</div>";
}
?>
Thanks in advanced, any assistance will be greatly appreciated!
values is Reserved Words in mysql it should be on backtick
SELECT item_name,image,price,added_by FROM `values`
And stop using mysql it is deprecated. Instead use mysqli or PDO
I'm new to php. Does not know how to use with expressions.
Here is a php code from search file of my site.
I want add html tags such as href, div, li, ul, etc. How could i do it?
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "Size: " . $row['fh_Vsize']. "<br />";
$output .= "Icon: <img src=" . $path .$row['fh_Sicon']. " alt=><br />";
$output .= "File ID: " . str_replace("_"," ",$row['fh_Sid']) . ""; $nbsp; $nbsp; $output .= "" . $row['fh_Vcaption'] . "<br />";
$output .= "Description: " . substr(strip_tags($row['fh_Sdescription']),0,150) . "<br />";
$output .= "Download: <a href=download_" . $row['fh_Sid'] . "/>Download</a><br /><br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchlink;
Something like this. I'm sorry for this code. I'm new here and will learn the basics:
<div class="info">
<h4>'.de_string($rec['fh_Sid']) .' ' .$rec['fh_Vcaption'] .' '. $rec['fh_Vextension']. '<span class="updated">updated</span></h4>
<div><img style="float:left;margin-right:5px" src="'. $path . str_replace("t_", "",$rec['fh_Sicon'] ).'"/></div><p>'. substr (strip_tags($rec['fh_Sdescription']),0,170) .' ...</p>
<ul class="prod-info">
<li class="none-separator">Last update: <strong>' . strftime("%d %b %Y", $rec['fh_Vdate']) . '</strong></li>
<li>License: '. $rec['fh_Vlicense'] . '</li>
<li>Size: <strong>'.$rec['fh_Vsize'].'</strong></li>
<li>Downloads: <strong>'. $views[$sid] .'</strong></li>
<li><img src="../../images/dl.png"/> Download</li>
</ul>
</div>
I am sorry this is not a full code
but basically you can use something like this
while ($line1 = mysqli_fetch_object($result2))
{
$i=0;
$id = $line1->$index; // fetching the whole object id of the SQL table
foreach ($line1 as $value1)
{
list($idName,$ext) = explode(".",$value1);
if ($i==1) // to show only the icon name
{
echo '
<li id='.$value1.' name="'.$id.'">
<form id="addchart'.$idName.'" action="addchart.php" target = "_self" method="post">
<a id='.$idName.' onmousedown = "AddorDelete(this.id,this.value)" href = "#" value='.$idName.'>
<img id="picture" class="icon" src="images/item/php.png" width="70" style="margin: 0px 0px 0px 0px"/>
<input type="hidden" name="db" value="'.$db.'" />
<input type="hidden" name="table" value="'.$table.'" />
<input type="hidden" name="id" value="'.$id.'" />
You could just echo 'html code and the attribute here'
be careful of the syntax to assign value, name or id if you want to assign the value, name or id with the predefined PHP variable you have made.
I prefer this way, which will let you have both php and html in their own format and easy to update:
<?php
if(mysqli_num_rows($results) >= 1)
{
while($row = mysqli_fetch_array($results))
{
?>
<div class="info">
<h4><?=de_string($rec['fh_Sid']) .' ' .$rec['fh_Vcaption'] .' '. $rec['fh_Vextension']?><span class="updated">updated</span></h4>
<div><img style="float:left;margin-right:5px" src="<?=$path . str_replace("t_", "",$rec['fh_Sicon'] )?>"/></div><p><?=substr (strip_tags($rec['fh_Sdescription']),0,170)?> ...</p>
<ul class="prod-info">
<li class="none-separator">Last update: <strong><?=strftime("%d %b %Y", $rec['fh_Vdate'])?></strong></li>
<li>License: <?=$rec['fh_Vlicense']?></li>
<li>Size: <strong><?=$rec['fh_Vsize']?></strong></li>
<li>Downloads: <strong><?=$views[$sid]?></strong></li>
<li><img src="../../images/dl.png"/> Download</li>
</ul>
</div>
<?php
}
}
else
echo "There was no matching record for the name " . $searchlink;
?>
I have put together a basic order list for admin users in php for checking order contents placed by logged in users.
The aim of this script is to retrieve the order details (item, quantity, price) as well as the user’s first name and surname (where ‘Order for:’ is).
The script below does everything ok in that it retrieves the order (and orders if there are more than one) and it’s/their item, quantity and price.
However, it doesn’t display the user’s name and surname.
I know the problem is that where I am trying to display the name is outside the while loop but Im a little stuck in where it should sit. Any suggestions? Code is below:
<?php
$page_title = 'View Individual Order';
include ('includes/header.html');
// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{ // Accessed through view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{ // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('./includes/header.html');
exit();
}
?>
<h1>Order Details</h1>
<?php
require_once ('database.php'); // Connect to the db.
// Retrieve the user's, order and product information.
$query = "SELECT us.users_id, us.users_sales_id, us.users_first_name, us.users_surname, us.users_dealer_name,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price,
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = mysql_query ($query) or die(mysql_error());
if (mysql_num_rows($result)) { // Valid user ID, show the form.
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
while($row = mysql_fetch_array($result, MYSQL_NUM)) { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
}// end of WHILE loop
echo '</table>
<p> Here:</p>
<br><br>
<p> << Back to Orders</p>
<p> </p>
<p> </p>
<p> </p>
';
} else { // Not a valid user ID.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close(); // Close the database connection.
?>
<p>Footer here</p>
<?php
include ('./includes/footer_admin_user.html'); // Include the HTML footer.
?>
One way you could do it is grab the row first, and then use a do/while loop instead of just a basic while loop. Like this:
if (mysql_num_rows($result)) { // Valid user ID, show the form.
/*********** I added this line ***********/
$row = mysql_fetch_array($result, MYSQL_NUM);
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
/*********** I changed this from a while loop to a do-while loop ***********/
do { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
} while($row = mysql_fetch_array($result, MYSQL_NUM)); // end of WHILE loop
It looks like the problem is when you're trying to display the user's name:
echo '<p>Order for:<strong>'.$row[2].' '.$row[3]
The $row variable doesn't exist yet. Not seeing your database or the result from your database query, my guess is that the user's name is repeated next to every single item in their order, so it might be as simple as just starting the WHILE loop, and checking to see if you've printed their name yet:
$lastUser = NULL;
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
if ($row[0] !== $lastUser) {
if (isset($lastUser)) {
// finish up the report for the previous user
}
// echo the stuff for the current user's name
$lastUser = $row[0];
}
// go on echo-ing their order information
}
// after the while loop is over,
// close up the last user's report
But like I said, this is just a guess, and might be totally off.
The problem is that you tried to access $row[2] and $row[3] before mysql_fetch_array(). Since you are already echo'ing HTML tags, why don't you "buffer" your output first like this?:
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
$order = '<tr bgcolor="' . $bg . '">
<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
$orders[$row[2] . " " . $row[3]][] .= $order;
}
Then do a second foreach loop for the $orders
foreach($orders as $name => $orderList)
{
echo "Order for: $name";
echo "<table ...>";
foreach($orderList as $order)
{
echo $order;
}
echo "</table>";
}