I am just trying to assign my results to an array but cannot see what i am doing wrong here as the results do not seem to be assigning Here is the code: // get all the recipes from the table $strippagename = str_replace("-", " ",$_GET['title']); $sql = "SELECT * FROM recipes WHERE category2 = '$strippagename'"; // looping through the table to get all results $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); $results = array(); $i=0; while (list($title) = mysql_fetch_row($res)) { $cat_url = strtr($title, "éèêà ëâúóÃáABCDEFGHIJKLMNOPQRSTUVWXYZ. ","eeeaeauoiaabcdefghijklmnopqrstuvwxyz--"); $cat_url = ereg_replace('[^a-zA-Z0-9_-]', '', $cat_url); $tmp = array( 'id' => $r['id'], 'title' => $title, 'caturl' => $cat_url ); $results[$i++] = $tmp; } Code (markup): Cheers, Adam
Adam, we are going to have to rewrite it a bit, but I got a question. Where is the $r['id'] defined at? Also, mysql_fetch_row only returns the [1] [2] [3] and not the named array varabiles. mysql_fetch_assoc is just the named and not the number. mysql_fetch_array is both named and number. $strippagename = str_replace("-", " ",$_GET['title']); $sql = "SELECT id,title FROM `recipes` WHERE `category2` = '$strippagename'"; $res = mysql_query($sql); $i = -1; while ($row = mysql_fetch_assoc($res)) { $cat_url = strtr($row['title'], "éèêà ëâúóÃáABCDEFGHIJKLMNOPQRSTUVWXYZ. ","eeeaeauoiaabcdefghijklmnopqrstuvwxyz--"); $cat_url = ereg_replace('[^a-zA-Z0-9_-]', '', $cat_url); $tmp = array( 'id' => $row['id'], 'title' => $row['title'], 'caturl' => $cat_url ); $results[$i++] = $tmp; } echo "<pre>"; print_r($results); echo "</pre>"; PHP:
Thanks very much for your reply. I have just put your code in there and it still does not seem to display anything :S Am i still doing something wrong? Cheers, Adam
Make sure there is a `category2` named the same thing as $strippagename is. Echo out the $sql and then run it in phpmyadmin to double check it.
Try this: $strippagename = str_replace('-', ' ', $_GET['title']); $sql = "SELECT id, title, category2 FROM `recipes` LIMIT 3"; $res = mysql_query($sql) or die(mysql_error()); $i = 0; while ($row = mysql_fetch_assoc($res)) { $cat_url = strtr($row['title'], 'éèêà ëâúóÃáABCDEFGHIJKLMNOPQRSTUVWXYZ. ', 'eeeaeauoiaabcdefghijklmnopqrstuvwxyz--'); $cat_url = ereg_replace('[^a-z0-9_-]', '', $cat_url); $tmp = array( 'id' => $row['id'], 'title' => $row['title'], 'category2' => $row['category2'], 'caturl' => $cat_url ); $results[$i++] = $tmp; } echo "\$strippagename: $strippagename<br />"; echo '<pre>'; print_r($results); echo '</pre>'; PHP: Let me know what output you get.
This has been returned: $strippagename: cheese ball Array ( [0] => Array ( [id] => 1 [title] => brie kisses [category2] => brie [caturl] => brie-kisses ) [1] => Array ( [id] => 2 [title] => sugar and glazed nut brie [category2] => brie [caturl] => sugar-and-glazed-nut-brie ) [2] => Array ( [id] => 3 [title] => tequila-cranberry chutney on brie [category2] => brie [caturl] => tequila-cranberry-chutney-on-brie ) ) Code (markup):
Ok, that confirmed the problem is in the WHERE clause. Hopefully, this will work, try script.php?title=brie as that is one that should surely return some results based on the output above. $strippagename = str_replace('-', ' ', mysql_real_escape_string($_GET['title'])); $sql = "SELECT id, title FROM `recipes` WHERE category2 = '$strippagename'"; $res = mysql_query($sql) or die(mysql_error()); $i = 0; while ($row = mysql_fetch_assoc($res)) { $cat_url = strtr($row['title'], 'éèêà ëâúóÃáABCDEFGHIJKLMNOPQRSTUVWXYZ. ', 'eeeaeauoiaabcdefghijklmnopqrstuvwxyz--'); $cat_url = ereg_replace('[^a-z0-9_-]', '', $cat_url); $tmp = array( 'id' => $row['id'], 'title' => $row['title'], 'caturl' => $cat_url ); $results[$i++] = $tmp; } echo "Searching for: $strippagename<br />"; echo "Found " . mysql_num_rows($res) . " results<br />"; echo '<pre>'; print_r($results); echo '</pre>'; PHP: Let me know if this outputs nothing, 0 results, or, of course, if it works
Thanks for that. This has returned 25 results which is great but i can't get them to display on the page. I am using smarty to assign the results using: $tmp = array( 'id' => $row['id'], 'title' => $row['title'], 'caturl' => $cat_url ); $results[$i++] = $tmp; // assigning the search results to smarty $smarty->assign('results', $results); Code (markup): Then this to display the results: {section name=nr loop=$results} <li><a href='/recipesdetails/{$cat_url}.html'>{$title}</a></li> {sectionelse} <b>Sorry there are no results with this criteria</b> {/section}</ul></p> Code (markup): But they are not displaying?
{foreach from=$results item=$item} <li><a href='/recipesdetails/{$item.cat_url}.html'>{$item.title}</a></li> {foreachelse} <b>Sorry there are no results with this criteria</b> {/foreach} Code (markup):
I am now getting the following error: Fatal error: Smarty error: [in recipe-subcats.tpl line 17]: syntax error: 'foreach: 'item' must be a variable name (literal string) (Smarty_Compiler.class.php, line 1174) in /home/mount/public_html/includes/libs/Smarty.class.php on line 1095 Code (markup):
As you can tell, I don't use Smarty The problem was that item=$item should not have the $ sign (am I the only one who thinks that is odd?) {foreach from=$results item=v} <li><a href='/recipesdetails/{$v.cat_url}.html'>{$v.title}</a></li> {foreachelse} <b>Sorry there are no results with this criteria</b> {/foreach} Code (markup):