Forum Moderators: coopster

Message Too Old, No Replies

Else if Problem

         

kevape

7:01 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Hello everybody, i am having problem with the IF statement what starts in the middle of this script, i want to check if a field is empty, if it is i want it to display "No Data" if it isnt i want the file name to display that is stored in the database.

// connect to db
$mysqli = mysql_connect(localhost,root,root);
@mysql_select_db(btc) or die( "Unable to select database");
$sql2="SELECT * FROM project_stages WHERE Project_ID = ".$project_id." ORDER BY Stage_No ASC";
$result2=mysql_query($sql2);

$num=mysql_numrows($result2);
// loop through the table called 'project_stages' and store the results in the variable chosen
$i=0;
while ($i < $num) {
$stage_no=mysql_result($result2,$i,"Stage_No");
$title=mysql_result($result2,$i,"Title");
$text=mysql_result($result2,$i,"Text");
$image=mysql_result($result2,$i,"Image");
$download=mysql_result($result2,$i,"Download");

//prin the results into a table
$display_block2 = "
<center>
<table class=logged_in_table border=0 bgcolor=#696969>
<td class=td_white colspan=2><b>Stage Title:</b> ".$stage_no." - ".$title."</td>
<tr>
<td class=td_blue colspan=2><b>Description & Status</b></td>
</tr>
<tr>
<td class=td_white colspan=2>".$text."</td>
</tr>
<tr>
<td class=td_blue><b>Image Download</b></td><td class=td_blue><b>File Download</b></td>
<tr>";

// if the image or download field is empty display 'No Data Exsists'
if ($result2 == "") {

$display_block2 .= "
<td class=td_white><p>No Data Exsists</p></td><td class=td_white><p>No Data Exsists</p></td>
</table>
<br>
<br>";

} else {
// or print the actual file names
$display_block2 .= "
<td class=td_white><a href=\"http://www.test.somedomain.co.uk/images/".$image."\">".$image."</a></td><td class=td_white>
<a href=\"http://www.test.somedomain.co.uk/downloads/".$download."\">".$download."</a></td>
</table>
<br>
<br>";

}
// now print the results
echo "$display_block2";
$i++;
}

?>

venelin13

7:21 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Instead of

if ($result2 == "") {

why do not use:

if ($image == "") {

The variable $result2 in your case is a database connection identifyer/resource. You are storing the value from the "Image" database table column into the $image variable, not into the $result2 variable.

kevape

7:30 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



thank you, but i want it to say "no data exsists" if there is no image and "no data exsists" if there is no dowload file. by using that code, it checks if there is an image record if there isnt it put "no data exsists" in both fields even when there is a download record. do you get me? i think it is because i ask it to

<td class=td_white><p>No Data Exsists</p></td><td class=td_white><p>No Data Exsists</p></td> creating 2 tds, when i just want the text read in. it is hard to put into words.

kevape

7:36 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



i basically wanna say

IF THE FIELDS ARE EMPTY?
if ($image == "" ¦¦ $download == "") {

SET THEM TO NO DATA EXSISTS
$image = "No Data Exsists";
$download = "No Data Exsists";

} else {

PRINT THEM AS THEY ARE?

venelin13

8:58 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



If you want to print the message in case the both fields are empty, use this:

if ($image == "" && $download == "") {

In case you want to print the message when just one of the field is empty, use this:

if ($image == "" ¦¦ $download == "") {