SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: A function

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Bucharest
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A function

    Hi!
    I have this code:

    <?php
    mysql_connect('localhost', '', '');
    mysql_select_db('');

    function Similar($num){

    $display = ''; // Empty the display string

    $keywords = explode("+", "$num");

    if ($keywords[0] != NULL)
    {
    $query = "SELECT * FROM ro_terms WHERE keyword LIKE '%".$keywords[0]."%'";

    for ($i=1;$i<count($keywords);$i++)
    {
    $query.= " OR keyword LIKE '%".$keywords[$i]."%'";
    }
    $query .= ' LIMIT 10';
    }

    $result = @mysql_query($query) or die(mysql_error());
    if(mysql_num_rows($result))
    {
    while($row = mysql_fetch_assoc($result))
    {
    $cuvant = $row['keyword'];
    $cuvant = str_replace("+", " ", $cuvant);
    $display = '<font size="2"><a href="http://cauta.bestweb.ro/'.$row['type'].'_'.$row['keyword'].'.html">'.$cuvant.'</a></font><br>';
    }
    }
    else
    {
    $display = 'Nu exista cautari similare!';
    }

    return $display; // Send the $display varaiable back
    // to the other page

    }

    echo 'sa'.Similar(mp3).'df';
    ?>

    This function is ok, but why it display me only one record? The limit is 20 so it must display 20 records!

    Why? And i don't have just one record for WEERE. I have 40 records for WHERE!

    Thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    order by id desc limit 10
    cheers

  3. #3
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    echo out the variable $query just after your lines

    PHP Code:
     $query .= ' LIMIT 10'
    }
     
    echo 
    $query//add this line 
    to see what query you are searching the DB with.

    Then paste and copy this into mysqladmin, or the command line on sql and see what result you get, if you get 40 records your codes at fault, if you get 1 record your querys at fault.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Bucharest
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for .Similar(music+mp3),
    echo $query:

    SELECT * FROM ro_terms WHERE keyword LIKE '%music%' OR keyword LIKE '%mp3%' LIMIT 10


    And with this query, using phpmyadmin i get 10 results!

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sosoro
    for .Similar(music+mp3),
    echo $query:

    SELECT * FROM ro_terms WHERE keyword LIKE '%music%' OR keyword LIKE '%mp3%' LIMIT 10

    The query is corect, no?
    Apart from the fact that when you first entered this post it had LIMIT 5 and now has LIMIT 10, why did it have limit 5 ? You have to cut and paste your results, not type them, you can type out errors subconcously.

    Anyway, that looks good, now CUT AND PASTE the result from your website into your DB SQL feature, see what results you get.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Bucharest
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I paste the code into phpmyadmin and i get 10 result!
    If i set limit to 40 i get 40 result!

    The query is ok!

    But don't work in this function

  7. #7
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well your while loop here
    while($row = mysql_fetch_assoc($result))
    {
    $cuvant = $row['keyword'];
    $cuvant = str_replace("+", " ", $cuvant);
    $display = '<font size="2"><a href="http://cauta.bestweb.ro/'.$row['type'].'_'.$row['keyword'].'.html">'.$cuvant.'</a></font><br>';
    }
    is overwritting the value of $display every loop so it will only display the last result.

    Also if you are going to use mysql and want search functionality I suggest you look into fulltext indexes (http://dev.mysql.com/doc/mysql/en/fulltext-boolean.html). They are much faster, more flexable, and easier to code than doing it all with OR and LIKE statements

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your $display overwites itself.

    change

    PHP Code:
    $display '<font size="2"><a href="http://cauta.bestweb.ro/'.$row['type'].'_'.$row['keyword'].'.html">'.$cuvant.'</a></font><br>'

    to

    PHP Code:
    $display .= '<font size="2"><a href="http://cauta.bestweb.ro/'.$row['type'].'_'.$row['keyword'].'.html">'.$cuvant.'</a></font><br>'
    Edit:

    pipped to the post, sorry OZROY
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Bucharest
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i understand.. first record will be replace be the second ...etc
    But how to write in this function, the php code ... to keep all records?

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    We cross posted see post #8
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Bucharest
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I change, but now i get a blank page

  12. #12
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Bucharest
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok.. work!
    I must go and buy a beer for evryone of you!

    Thanks guys!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •