if, else and while

Reply

Join Date: Sep 2008
Posts: 1
Reputation: soddengecko is an unknown quantity at this point 
Solved Threads: 0
soddengecko soddengecko is offline Offline
Newbie Poster

if, else and while

 
0
  #1
Sep 4th, 2008
Hi All

I am having a bit fo an issue with an if/else statement inside a while statement. Basically there are two while statements. one to retrieve a distinct value, the second to list all links associated to that distinct value.

I am trying to build a favicon fetcher to display beside each link. I can do this part, but need to have a "no favicon" image displayed if the site does not have one, or it is placed somewhere other than the root of the domain.

i looked up if statements inside while statements but keep getting unexpected { errors.

is there anyone here who can see what i am doing wrong?

TIA

  1. function links ()
  2. {
  3. //Select the distinctive CATEGORY titles from the CATEGORIES table
  4. $sqlcat = 'SELECT DISTINCT category FROM ex_links ORDER BY category ASC';
  5. $resultcat = mysql_query($sqlcat) or die('There are currently no links in the database.');
  6. while ($rowcat = mysql_fetch_assoc($resultcat)){
  7. //Begin the HTML formatting and output the CATEGORY title
  8. echo "<div class='linkholder'>\n";
  9. echo "<div class='linktitles'>";
  10. echo "<h2>".$rowcat['category']."</a></h2>\n";
  11. echo "</div>\n";
  12. echo "<div id=\"navcontainer\">\n\n";
  13. // Now we have the Category title, grab all links associated with that
  14. // title and place them under the correct heading.
  15. $sql = 'SELECT * FROM ex_links WHERE category="'.$rowcat['category'].'" ORDER BY title';
  16. $result = mysql_query($sql) or die('There are currently no links in the database.');
  17. while ($row = mysql_fetch_assoc($result)){
  18. $id = $row["id"];
  19. $url = $row["url"];
  20. $title = $row["title"];
  21. $ident = $row["id"];
  22. $category = $row["category"];
  23. $target = $row["target"];
  24. echo "<div class=\"icoleft\"><a href=\"$url\" target=\"$target\"><img src=\"$url/favicon.ico\" height='16px' width='16px' /></a></div>\n";
  25. echo "<div class=\"linkleft\"><a href=\"$url\" target=\"$target\">$title</a></div>\n";
  26. echo "<div class=\"linkright\"><a href=\"admin/links_delete.php?identity=$id\"><img src=\"images/cancel.png\" /></a></div>\n";
  27. echo "<div class=\"clear\"></div>\n";
  28. }
  29. ?>
  30. <script>
  31. function dodeletelink( ) {
  32. new Ajax.Updater( 'linkmanager', 'admin/links_delete.php', { method: 'post', parameters: $('').serialize() } );
  33. $('').reset();
  34. }
  35. </script>
  36. <?php
  37. //End the HTML formatting for the links and categories
  38. echo "</div>\n";
  39. echo "</div>\n\n";
  40. }
  41. }
  42. ?>

this is the code i would like to have executed inside the second while statement

  1. $filename = "$url/favicon.ico";
  2.  
  3. if (file_exists($filename)) {
  4. print "The file $filename exists";
  5. } else {
  6. print "The file $filename does not exist";
  7. }
Last edited by soddengecko; Sep 4th, 2008 at 7:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: if, else and while

 
0
  #2
Sep 5th, 2008
I might suggest using some code formatting and maybe even an editor with syntax highlighting ... if this is the real script, it looks like maybe you missed actually closing your while loops ...
  1. function links () {
  2. //Select the distinctive CATEGORY titles from the CATEGORIES table
  3. $sqlcat = 'SELECT DISTINCT category FROM ex_links ORDER BY category ASC';
  4. $resultcat = mysql_query($sqlcat) or die('There are currently no links in the database.');
  5.  
  6. while ( $rowcat = mysql_fetch_assoc( $resultcat ) ) {
  7.  
  8. //Begin the HTML formatting and output the CATEGORY title
  9. echo "<div class='linkholder'>\n";
  10. echo "<div class='linktitles'>";
  11. echo "<h2>".$rowcat['category']."</a></h2>\n";
  12. echo "</div>\n";
  13. echo "<div id=\"navcontainer\">\n\n";
  14.  
  15. // Now we have the Category title, grab all links associated with that
  16. // title and place them under the correct heading.
  17. $sql = 'SELECT * FROM ex_links WHERE category="'.$rowcat['category'].'" ORDER BY title';
  18. $result = mysql_query($sql) or die('There are currently no links in the database.');
  19.  
  20. while ( $row = mysql_fetch_assoc( $result ) ) {
  21. $id = $row["id"];
  22. $url = $row["url"];
  23. $title = $row["title"];
  24. $ident = $row["id"];
  25. $category = $row["category"];
  26. $target = $row["target"];
  27. echo "<div class=\"icoleft\"><a href=\"$url\" target=\"$target\"><img src=\"$url/favicon.ico\" height='16px' width='16px' /></a></div>\n";
  28. echo "<div class=\"linkleft\"><a href=\"$url\" target=\"$target\">$title</a></div>\n";
  29. echo "<div class=\"linkright\"><a href=\"admin/links_delete.php?identity=$id\"><img src=\"images/cancel.png\" /></a></div>\n";
  30. echo "<div class=\"clear\"></div>\n";
  31. } // THIS WAS MISSING
  32. } // THIS WAS MISSING
  33. }
  34. ?>

I didn't really look at any of the code, I just noticed this problem -- hope it helps
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC