hide empty rows inside of li - php

I have a li with a couple of row..I can hide row's when in db have NULL, but when have empty I don't know how can do it.
this is the code I used:
<ul id="responds">
<?
$sql = $conn->prepare("SELECT id_diagnosticon, f_diagnosticon, id_paciente, id_doctor, diagnostico, diagnostico1, diagnostico2, diagnostico3, hconsulta2, presion_art, peso FROM DIAGNOSTICON where id_paciente = $_GET[id_paciente] order by id_diagnosticon DESC");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
echo '<li id="item_'.$row["id_diagnosticon"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnosticon"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnosticon"]; echo ' <br><br> ';
if (!is_null($row["diagnostico"]))
{
echo '<b>Diagnostico de consulta :</b>'; echo $row["diagnostico"]; echo '<br><br>';
}
if (!is_null($row["diagnostico1"]))
{
echo '<b>Diagnostico 2 :</b>'; echo $row["diagnostico1"]; echo '<br><br>';
}
if (!is_null($row["diagnostico2"]))
{
echo '<b>Diagnostico 3 :</b>'; echo $row["diagnostico2"]; echo '<br><br>';
}
if (!is_null($row["diagnostico3"]))
{
echo '<b>Diagnostico 4 :</b>'; echo $row["diagnostico3"]; echo '<br><br>';
}
if (!is_null($row["presion_art"]))
{
echo '<b>Presi&oacuten Arterial : </b>'; echo $row["presion_art"];
}
if (!is_null($row["peso"]))
{
echo ' | <b> Peso : </b>'; echo $row["peso"];
}
echo '<br><br>';
echo $row["hconsulta2"].'</li>';
}
?>
</ul>
so I want to hide these row in li when the data in mysql in empty....right now only hide NULL data
-

Use empty:
if ( ! empty($row['something']) )
{
echo $row['something'];
}

Another option is to handle this with the query using COALESCE:
WHERE COALESCE(SomeField,'') <> ''

Related

How to check table cell in database and output results?

I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post

PHP / MySQL - Query not fetching any data

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

How do select one element in a while loop (Re-assign a variable inside a while loop)

The program "loops and returns" all the entries in in a folder on the server.
but my problem is: When the user selects any of the returned entries, only the first element is returned.
How do I return the user selected entry?
p.s I'm a noob at php
modal refers to "bootstrap modal"
<?php
include_once("include/main.inc.php");
$query ="SELECT job_id, job_logo, job_company, job_title, job_area, job_county FROM job_tbl";
$result = mysql_query($query) or die (mysql_error());
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_array($result)){
$id = $row['job_id'];
$d = $row['job_logo'];
echo '<div class="col-xs-6 col-md-6">';
echo '<a href ="#" ><h6>'.$row['job_title'] .'</h6> </a>';
echo 'View';
echo '</div>';
}
$memid = $row['member_id'];
$query2 ="SELECT job_id, member_id, job_logo, job_category, job_description, job_skills, emp_type, job_experiance, job_vacancy, job_salary, expery_date, job_contact_name FROM job_tbl";
$modalResult = mysql_query($query2) or die (mysql_error());
if (!$modalResult) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_array($modalResult)){
$d = $row['job_logo'];
echo '<div class = "modal fade" id= "view" role ="dialog">';
echo '<div class ="modal-dialog">';
echo '<div class = "modal-content">';
echo '<div class ="modal-header">';
echo '<h4>Contact Locate.ie</h4>';
echo '</div>';
echo '<div class= "modal-body">';
echo '<div class ="container">';
echo '<div class="jumbotron">';
echo '<legend class= "scheduler-border">';
echo '<h5>Description:</h5>';
echo '<h6>'.$row['job_description'].'</h6>';
echo '</legend>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div></div></div>';
}
?>
You wrote :
while($query2 = mysql_fetch_array($modalResult))
{
$d = $row['job_logo'];
echo "<img src=\"http://www.foo.ie/images/job/".$d."\" >";
}
It's probably a copy/paste typo. Shouldn't it be :
while($query2 = mysql_fetch_array($modalResult))
{
$d = $query2['job_logo'];
echo "<img src=\"http://www.foo.ie/images/job/".$d."\" >";
}
Also, as adviced before, try to learn PDO/prepared statements

If empty variable

I'm using the code below:
<?php
$sql = "SELECT * FROM nieuws ORDER BY id DESC LIMIT 0, 3";
$results = $db->query($sql);
if (empty($results)) {
echo 'Momenteel geen nieuws.';
}
foreach ($results as $row)
{
echo '<h3>';
echo $row['titel'].'<br>';
echo '</h3>';
echo $row['bericht'].'<br>';
echo '<br><br><div style="float: left;"><small>Schrijver: '.$row['schrijver'].'</small></div>';
echo '<div style="float: right; margin-bottom:-20px;">Lees meer...</div><br><br><hr>';
}
?>
This code will fetch information from the database. But if there is nothing in a table, it needs to say 'Momenteel geen nieuws.'.
It doesn't say that, and i don't understand why it doesn't...
If there is somebody who can help me out, it would be very nice!
<?php
$sql = "SELECT * FROM nieuws ORDER BY id DESC LIMIT 0, 3";
$results = $db->query($sql);
if (count($results) < 1) {
echo 'Momenteel geen nieuws.';
}else
{
foreach ($results as $row)
{
echo '<h3>';
echo $row['titel'].'<br>';
echo '</h3>';
echo $row['bericht'].'<br>';
echo '<br><br><div style="float: left;"><small>Schrijver: '.$row['schrijver'].'</small></div>';
echo '<div style="float: right; margin-bottom:-20px;">Lees meer...</div><br><br><hr>';
}
}
?>
It's not empty even with no results. You can try count() instead perhaps?
if (count($results) <1 ) {
echo 'Momenteel geen nieuws.';
}
A variable is considered empty if it does not exist or if its value
equals FALSE.
http://www.php.net/empty
Try this
<?php
$sql = "SELECT * FROM nieuws ORDER BY id DESC LIMIT 0, 3";
$results = $db->query($sql);
if (!$results) {
echo 'Momenteel geen nieuws.';
}//end if
else{
foreach ($results as $row){
echo '<h3>';
echo $row['titel'].'<br>';
echo '</h3>';
echo $row['bericht'].'<br>';
echo '<br><br><div style="float: left;"><small>Schrijver: '.$row['schrijver'].'</small></div>';
echo '<div style="float: right; margin-bottom:-20px;">Lees meer...</div><br><br><hr>';
}//end foreach
}//end if else
?>

If statemen in the wrong place

Please you help? I run a query via search form and output results, although, it is not displaying as it should? Still new to php so please excuse if my code is....
the result needed. If te search returns I want one of 2 things. To either show Employed by: xxx or Not currently employed:
<?php
if ($_REQUEST['submit']) {
$term = $_POST['term'];
$row ['employerid'] == $user_data ['user_id'];
$XX = "<br><br><div class='messagebox'><h2> <center> Oops! </h2> <p>We were only to retrieve a partial record on <strong>$term</strong> you have entered. Please make use of our contact form if you would like us to get you your reference. Be sure to enter the three required fields. <a href='Mailforms/refrequest.php' class='lightbox'>Click Here!</a> or to validate the id <a href='idverification.php'> Click here</a></p>
<br />
</div>";
$sql = mysql_query("select * from ref_employees where `idnumber`= '$term'")
or die('Error in query : $sql. ' .mysql_error());
{
if (mysql_num_rows($sql) > 0 )
{
while ($row = mysql_fetch_array($sql)){
if ($row ['employed'] == '1') {
echo '<h4>Currently Employed By : '.$row['companyname'];
echo '</h4> ';}
if ($row ['employed'] == '0') {
echo '<h4>Not Currently employed : '.$row['companyname'];
echo '</h4> ';}
echo '<h4> ID : '.$row['idnumber'] ;
echo '<br> First Name : '.$row['firstname'];
echo '<br> Last Name : '.$row['lastname'];
echo '<br> Gender : '.$row['gender'];
echo ' </h4>';
echo '<br />';
echo '<h2>Some Additional Options</h2>';
echo '<br />';
}
}
else
{
print ("$XX");
}
mysql_free_result($sql);
mysql_close($connection);
}
You have put an extra opening curly brace { above if (mysql_num_rows($sql) > 0 ) {
Corrected Code:
<?php
if ($_REQUEST['submit']) {
$term = $_POST['term'];
$row ['employerid'] == $user_data ['user_id'];
$XX = "<br><br><div class='messagebox'><h2> <center> Oops! </h2> <p>We were only to retrieve a partial record on <strong>$term</strong> you have entered. Please make use of our contact form if you would like us to get you your reference. Be sure to enter the three required fields. <a href='Mailforms/refrequest.php' class='lightbox'>Click Here!</a> or to validate the id <a href='idverification.php'> Click here</a></p>
<br />
</div>";
$sql = mysql_query("select * from ref_employees where `idnumber`= '$term'")
or die('Error in query : $sql. ' .mysql_error());
if (mysql_num_rows($sql) > 0 ) {
while ($row = mysql_fetch_array($sql)){
if ($row ['employed'] == '1') {
echo '<h4>Currently Employed By : '.$row['companyname'];
echo '</h4> ';}
if ($row ['employed'] == '0') {
echo '<h4>Not Currently employed : '.$row['companyname'];
echo '</h4> ';}
echo '<h4> ID : '.$row['idnumber'] ;
echo '<br> First Name : '.$row['firstname'];
echo '<br> Last Name : '.$row['lastname'];
echo '<br> Gender : '.$row['gender'];
echo ' </h4>';
echo '<br />';
echo '<h2>Some Additional Options</h2>';
echo '<br />';
}
}
else
{
print ("$XX");
}
mysql_free_result($sql);
mysql_close($connection);
}

Categories