webguync Posted June 15, 2007 Share Posted June 15, 2007 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> Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 15, 2007 Share Posted June 15, 2007 in a MySQL row which has a value of [10] What do you mean value of [10], is that column(field) name, the value in the filed or what ? Quote Link to comment Share on other sites More sharing options...
MrSheen Posted June 15, 2007 Share Posted June 15, 2007 Your just missing a few punctuation if ( $row['10'] == 1 ) { echo "<td>".$img."</td>"; } else { echo "<td></td>"; } Quote Link to comment Share on other sites More sharing options...
webguync Posted June 15, 2007 Author Share Posted June 15, 2007 [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? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 15, 2007 Share Posted June 15, 2007 Just a quick question for my own edification. Array values always start with [0], as i've been told. Therefore, wouldn't the f15 position be [9] instead of [10]? Quote Link to comment Share on other sites More sharing options...
webguync Posted June 15, 2007 Author Share Posted June 15, 2007 yes, I believe you are correct with that Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 15, 2007 Share Posted June 15, 2007 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>"; } Quote Link to comment Share on other sites More sharing options...
per1os Posted June 15, 2007 Share Posted June 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 15, 2007 Share Posted June 15, 2007 Good stuff to know, frost. Quote Link to comment Share on other sites More sharing options...
webguync Posted June 15, 2007 Author Share Posted June 15, 2007 I am still having trouble with this. The if is being passed by even if the condition is met, and the else is displaying for all records. any ways to troubleshoot why this is happening? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 15, 2007 Share Posted June 15, 2007 Post the code you're using now. Quote Link to comment Share on other sites More sharing options...
webguync Posted June 15, 2007 Author Share Posted June 15, 2007 <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 Quote Link to comment Share on other sites More sharing options...
webguync Posted June 16, 2007 Author Share Posted June 16, 2007 any ideas on this? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 16, 2007 Share Posted June 16, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
webguync Posted June 16, 2007 Author Share Posted June 16, 2007 it's not displaying anything...but in MySQL there is a row named "importance" that does have entries with values of 1, so I guess I am missing something somewhere else? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 16, 2007 Share Posted June 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
webguync Posted June 16, 2007 Author Share Posted June 16, 2007 $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? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 16, 2007 Share Posted June 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
webguync Posted June 17, 2007 Author Share Posted June 17, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 17, 2007 Share Posted June 17, 2007 But your WHERE clause is narrowing it down to one entry: WHERE id = '$_GET[id]' If the id's are unique then you're only going to pull one person from that table. Quote Link to comment Share on other sites More sharing options...
webguync Posted June 17, 2007 Author Share Posted June 17, 2007 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> Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 17, 2007 Share Posted June 17, 2007 This is a tricky one. Ok, let's try this. Since you're assigning the 'row' results to variables let's try using that in your 'if' statement. if ( $importance == 1 ) { Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 17, 2007 Share Posted June 17, 2007 tips bro: if you if the if statement is not working maybe the values arent right so i suggest print the whole query\ say: echo '<pre>'; print_r($result); echo '</pre>'; hope that helps;;;;;; Quote Link to comment Share on other sites More sharing options...
webguync Posted June 18, 2007 Author Share Posted June 18, 2007 thanks simcoweb using: if ( $importance == 1 ) { was indeed the right solution Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 18, 2007 Share Posted June 18, 2007 Cool! Yeah, once I saw the full code you had set a value for that variable so it made sense to use it since $row['importance'] had been assigned to it. Glad we got it! Be sure to mark as Solved! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.