If empty variable - php

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
?>

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

Only the result result from php query displays in the html table, the rest display outside

I'm building a forum for learning purposes. I'm trying without success to retrieve forum categories from the database and displaying them in a table, but only the first category displays in the table, the rest display outside the table. I will post my code, and a screenshot of the image.
<?php
include 'connect.php';
include 'header.php';
$sql = "SELECT categories.cat_id,categories.cat_name,
categories.cat_description, COUNT(topics.topic_id) AS topics
FROM categories
LEFT JOIN topics ON topics.topic_id = categories.cat_id
GROUP BY categories.cat_name, categories.cat_description,
categories.cat_id";
$result = mysql_query($sql);
if(!$result) {
echo 'The categories could not be displayed, please try again later.';
} else {
if(mysql_num_rows($result) == 0) {
echo 'No categories defined yet.';
} else {
//prepare the table
echo '
<div class="container">
<table class="table forum tale-striped table-hover">
<thead>
<tr>
<th class="cell-stat"></th>
<th><h3>Category</h3></th>
<th><h3>Last topic</h3></th>
</tr>
</thead>';
while($row = mysql_fetch_assoc($result)) {
echo '<tbody>';
echo '<tr >';
echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger"> </i></td>';
echo '<td><h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'].'</td>';
echo '<td class="float-xs-right">';
//fetch last topic for each cat
$topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC
LIMIT 1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult) {
echo 'Last topic could not be displayed.';
} else {
if(mysql_num_rows($topicsresult) == 0) {
echo 'no topics';
} else {
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>'; //container
}
}
}
include 'footer.php';
?>
https://www.dropbox.com/s/c1dgijuafgv9jzu/Capture.PNG?dl=0
Quite simply you have the closing </table> tag inside the while loop so once the first row is output you close the table. Just move it outside the while loop like this, also the opening <tbody> need moving above the while loop as well
<?php
include 'connect.php';
include 'header.php';
$sql = "SELECT categories.cat_id,categories.cat_name,
categories.cat_description, COUNT(topics.topic_id) AS topics
FROM categories
LEFT JOIN topics ON topics.topic_id = categories.cat_id
GROUP BY categories.cat_name, categories.cat_description,
categories.cat_id";
$result = mysql_query($sql);
if(!$result) {
echo 'The categories could not be displayed, please try again later.';
} else {
if(mysql_num_rows($result) == 0) {
echo 'No categories defined yet.';
} else {
//prepare the table
echo '
<div class="container">
<table class="table forum tale-striped table-hover">
<thead>
<tr>
<th class="cell-stat"></th>
<th><h3>Category</h3></th>
<th><h3>Last topic</h3></th>
</tr>
</thead>
<tbody>'; //<-- moved
while($row = mysql_fetch_assoc($result)) {
echo '<tr >';
echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger"> </i></td>';
echo '<td><h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'].'</td>';
echo '<td class="float-xs-right">';
//fetch last topic for each cat
$topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC
LIMIT 1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult) {
echo 'Last topic could not be displayed.';
} else {
if(mysql_num_rows($topicsresult) == 0) {
echo 'no topics';
} else {
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
}
}
echo '</tbody>'; //<-- moved
echo '</table>'; //<-- moved
echo '</div>'; //container //<-- moved
}
include 'footer.php';
?>
Becuse you're closing the <table> tag within the while-loop. Also, the <tbody> should be outside the loop as well, since there should only be one <tbody> in a table like this.

how to get multiple values of drop down from a loop?

<?php
// this is where selecting of number of rooms in database
$sql = mysqli_query($con, 'SELECT * FROM roomtype');
$get_room1 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=2");
$get_room2 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=3");
$get_room3 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=4");
$get_room4 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=5");
$standard = mysqli_num_rows($get_room1);
$deluxe = mysqli_num_rows($get_room2);
$family = mysqli_num_rows($get_room3);
$regular = mysqli_num_rows($get_room4);
// this is the loop for the drop down and it will display the number of rooms available
while($row = mysqli_fetch_array($sql))
{
$a=$row['type_id'];
$b = $row['rmtype'];
$_SESSION['room_type'] = $row['rmtype'];
echo '<div style="height: 300px;">';
echo '<div style="float: left; width: 100px; margin-top: 15px; margin-left: 60px;">';
echo "<img width=250 height=200 alt='Unable to View' src='" . $row["image"] . "'>";
echo '</div>';
echo '<div class="well" style="float: right; width: 780px; margin-top: 5px;">';
echo '<div style="float: right;">';
echo '</div>';
echo '<br />';
echo "<label style='margin-left: px;'>Number of rooms:";
echo "<select name='select_no_rooms[]' value='" .$row['rmtype']." ' />";
echo "<option> </option>";
if ($row['rmtype']=="Standard Room")
{
for ($x = 1; $x<=$standard; $x++){
echo "<option name='standard'>$x</option>";}
}
if ($row['rmtype']=="Deluxe Room")
{
for ($x = 1; $x<=$deluxe; $x++){
echo "<option name='deluxe'>$x</option>";}
}
if ($row['rmtype']=="Family Room")
{
for ($x = 1; $x<=$family; $x++){
echo "<option name='family'>$x</option>";}
}
if ($row['rmtype']=="Regular Room")
{
for ($x = 1; $x<=$regular; $x++){
echo "<option name='regular'>$x</option>";}
}
echo "</select>";
}
?>
This is the design of drop down,
add [ ] tag to name tag, so you will get your value in array.
<option name='deluxe[]'>$x</option>
First thing that I notice is that in you select statement, you cannot have value as an attribute. Please check the documentation select tag
And the option tag does not include name attribute. Please check the documentation option tag
If you want to get multiple values from a drop down list. Please check this article

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

hide empty rows inside of li

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,'') <> ''

Categories