Jump to content

[SOLVED] repost: need to display image within if/else statement


webguync

Recommended Posts

I believe I posted this previously, but didn't get a reply which is the reason for the repost. This should be fairly simple, but I cannot figure it out!

 

I have an image path delared in a variable

 

$img= "<img src='icons/apo.gif' width='15' height='17' alt='airportindicator'>";

 

and I only want the image to display if a condition is met in a MySQL row which has a value of [10]. I have tried:

 

if ( $row[10] == 1 ) {

echo "<td>$img</td>";

} else {

echo "<td></td>";

}

 

but I believe the syntax is wrong. If the condition is not met, I want a blank value within the html <td></td>

Link to comment
Share on other sites

[10] would be the position in the SQL (f15)

 

$sql = "SELECT f2,f3,phone,f8,f9,f10,f11,f12,f13,f15 FROM station ORDER BY f11 ASC" ;

 

one more question,

 

in the table structure, I now have:

echo"<tr><td>$img</td>

 

what is the best way to add the if else statement to that in order for the result to appear where $img currently is?

 

 

Link to comment
Share on other sites

I believe your other question was answered by Mr. Sheen about the if/else statement.

 

if ( $row['9'] == 1 ) {

  echo "<td>".$img."</td>";

} else {

  echo "<td>Image Not Available.</td>";

}

 

I don't think it was. That is pulling the literal value at index '9'  not the actual index of 9 as 9 and '9' are 2 different items.

 

if ( $row[9] == 1 ) {
   echo "<td>".$img."</td>";
} else {
   echo "<td>Image Not Available.</td>";
}

 

Is what you want if you are using the actual index of the 0,1,2,3,4...etc If he wanted to call the f15 column this would also work.

 

if ( $row['f15'] == 1 ) {
   echo "<td>".$img."</td>";
} else {
   echo "<td>Image Not Available.</td>";
}

 

As long as the array is associative, that is the literal index.

 

Link to comment
Share on other sites

<tr>

<?

if ( $row['importance'] == 1 ) {

  echo "<td colspan='3'>".$img."</td>";

} else {

  echo "<td colspan='3'>not so important.</td>";

}

?>

      </tr>

 

also, I have image set as :

 

$img= "<img src='images/mymage.gif' width='25' height='25' alt='myimage'>";

 

the field in MySQL is named 'importance' and some of the values in that field = 1. That is when I want the image to display.

 

<?

if ( $row[10] == 1 ) {

  echo "<td colspan='3'>".$img."</td>";

} else {

  echo "<td colspan='3'>not so important.</td>";

}

?>

 

since 10 is the array association order

 

Link to comment
Share on other sites

If it's 'bypassing' the if then that probably means that condition is not being met. Only way to find out is to echo that to see if there's actually a value set. I think this will work and if displays the value of that field then the value is being set. Otherwise, if it's not then nothing is matching up with 1:

 

<tr>
<?
if ( $row['importance'] == 1 ) {
   $test = $row['importance'];
   echo $test;
   echo "<td colspan='3'>".$img."</td>";
} else {
   echo "<td colspan='3'>not so important.</td>";
}
?>
      </tr>

also, I have image set as :

$img= "<img src='images/mymage.gif' width='25' height='25' alt='myimage'>";

the field in MySQL is named 'importance' and some of the values in that field = 1. That is when I want the image to display.

<?
if ( $row[10] == 1 ) {
   echo "<td colspan='3'>".$img."</td>";
} else {
   echo "<td colspan='3'>not so important.</td>";
}
?>

Link to comment
Share on other sites

I think we need to look at your query for this. There query should contain a WHERE clause related to that field and the value. Once it pulls the correct data then the 'while' loop can be tweaked to display it. The if/else will probably need a tweak or two depending. Post your query code. At least we figured out it's not setting the value.

 

Link to comment
Share on other sites

$sql = "SELECT f_name, l_name,title, address1, city,  prim_tel,  email, URL, last_updated, company, importance, notes
FROM $table_name WHERE id = '$_GET[id]' ";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$img= "<img src='images/Recruiter.gif' width='25' height='25' alt='Recruiter'>"; 
while ($row = mysql_fetch_array($result)) {

 

I don't have a query specifically set up for that field [importance]

 

do I need to do a separate query for this?

Link to comment
Share on other sites

You don't need to make it specific as long as you either name that field in the query, use the * for all, or.. name it in the WHERE clause. Since i'm not 100% sure on what your form is doing I can see from the query that you're passing the 'id' via the url and using that to select the proper item from the database. Now, if you were to add AND importance='1' to it then the screening would be done in the query. Then use an if/else to display if the rows come back showing true:

 

if($result == 1) {

echo "blah blah";

} else {

echo "no love here";

}

 

Something like that.

Link to comment
Share on other sites

well basically I have a table which displays the results from the query:

$sql = "SELECT f_name, l_name,title, address1, city,  prim_tel,  email, URL, last_updated, company, importance, notes

FROM $table_name WHERE id = '$_GET[id]'  ";

 

and I don't want to filter those results to just display the ones with importance=1, I just want the records that meet that criteria to display an image in a td cell, and those that  do not, to not display anything.

 

so when I add AND importance='1' to the above query that gives me blank results for most of the records, which I don't want.

 

I do want to meet this condition, however, and the if is still not working, so 'Not so Important' is always displaying even when the result of importance is equal to 1 ...

<?
if ( $row['importance'] == 1 ) {
   
   echo "<td colspan='3'>".$img."</td>";
} else {
   echo "<td colspan='3'>Not so Important</td>";
}
?>

Link to comment
Share on other sites

yes, the ID's are Unique and I only want one display per page

 

maybe displaying the entire code on the page will help

<?php
if (!$_GET[id]) {
    header ( "Location: http://www.mywebsite.com");
} else {
     session_start();
}
if ($_SESSION[valid] !="yes") {

header ("Location: http://www.mywebsite.com");
exit;
}

//check for validity of user
$db_name="my_DB";
$table_name ="myTable";
$connection = @mysql_connect("localhost", "username", "password")
or die (mysql_error());
$db = @mysql_select_db($db_name, $connection) or die (mysql_error());

$chk_id = "SELECT id FROM $table_name WHERE id ='$_GET[id]' ";
$chk_id_res = @mysql_query($chk_id, $connection) or die(mysql_error());
$chk_id_num = mysql_num_rows($chk_id_res);
if ($chk_id_num !=1) {
header ("header://www.mywebpage/login.php");
exit;
} else {
$sql = "SELECT f_name, l_name,title, address1, city,  prim_tel,  email, URL, last_updated, company, importance, notes
FROM $table_name WHERE id = '$_GET[id]'  ";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$img= "<img src='images/airplane.gif' width='25' height='25' alt='Airplane'>"; 
while ($row = mysql_fetch_array($result)) {
$f_name=$row[0];
$l_name=$row[1];
$title=$row[2];
$address1=$row[3];
$city=$row[4];                     

$prim_tel=$row[5];
$email=$row[6];
$URL=$row[7];
$last_updated=$row[8];
$company=$row[9];
$importance=$row[10];
$notes=$row[11];
      }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>Contact Database</title>



</head>
<body>


<!-- begin outer wrapper div --> 

<div id="wrapper_outer">

<!-- begin header div --> 
<!-- top nav list --> 


<h2>Contact details for <? echo "$f_name $l_name who works for <span class=\"company\">$company"; ?></span></h2>
<table id="recruiter_list" cellspacing="0">
<tr>
<th colspan="3" align="center">CONTACT INFORMATION</th>
</tr>
<tr>
<?
if ( $row['importance'] == 1 ) {
   
   echo "<td colspan='3'>".$img."</td>";
} else {
   echo "<td colspan='3'>Not so Important</td>";
}
?>


      </tr>
<td>
<? echo "$f_name $l_name"; ?>,<br /><h2><?  echo  "$company $importance" ?></h2></td>
<td><strong>Title</strong><br /><? echo "<em> $title </em>"; ?></td>

<td><strong>Address</strong><br /><? echo "$address1"; ?></td>
</tr>
<tr>
<td><strong>Location</strong><br /><? echo "$city"; ?></td>
<td><strong>Telephone:</strong><br /><? echo "$prim_tel";  ?></td>


<td ><strong>e-mail:</strong><br /> <? echo  "<a href=\"mailto:$email\" class=\"recruiter\">$email</a>"; ?></td>
           
</tr>
<tr id='trX'><td colspan='3'><a href='#' onClick="FuncShowHide()"> Display URL</td> 
</tr> 
<tr id='hideShow' style='display:none'>
<td colspan="3" ><strong>URL:</strong><br /> <? echo  "<a href=\"http://$URL\" class=\"recruiter\">$URL</a>"; ?></td>
<tr>
<td colspan="3" align=\"center"\>
<strong>Notes</strong><br />
<? echo "$notes";  ?>
</td>
</tr>
<tr>
<td colspan="3">Last Update: <?php echo "$last_updated" ;?>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<p><a href="Recruiters.php">Return to main menu</a></p></td>
</tr>

</table>    
<?php
//close the db
mysql_close();
?>
<div id="clear_float"
<?php require('includes/footer_tag.php') ; ?>
<?php require('includes/redirect.php'); ?>
</div>
</div>
<!-- begin footer --> 

</div>
</div>

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.