What am i doing wrong here?

Discussion in 'PHP' started by adamjblakey, Aug 3, 2007.

  1. #1
    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
     
    adamjblakey, Aug 3, 2007 IP
  2. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #2
    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:
     
    exodus, Aug 3, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    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
     
    adamjblakey, Aug 3, 2007 IP
  4. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #4
    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.
     
    exodus, Aug 3, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I have just checked in phpma and it comes back with results but not on the site.
     
    adamjblakey, Aug 3, 2007 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Can anyone solve this?
     
    adamjblakey, Aug 6, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    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.
     
    krt, Aug 6, 2007 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    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):
     
    adamjblakey, Aug 6, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #9
    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 :p
     
    krt, Aug 6, 2007 IP
  10. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #10
    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?
     
    adamjblakey, Aug 6, 2007 IP
  11. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #11
    {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):
     
    krt, Aug 6, 2007 IP
  12. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #12
    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):
     
    adamjblakey, Aug 6, 2007 IP
  13. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #13
    As you can tell, I don't use Smarty :p
    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):
     
    krt, Aug 6, 2007 IP
  14. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Works a treat.. thanks a lot for your help.
     
    adamjblakey, Aug 6, 2007 IP