SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict adam2003w's Avatar
    Join Date
    Mar 2004
    Location
    colorado
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nested Loop/Recordset Problems with "Like" Comparison

    I'm trying to created a nested loop/recordset to show data organized on a page. In the past I've compared a cell in a table with a parent number and see if they equal like so (and it works great):

    PHP Code:
    WHERE rs_sub.parent_cell rs_category.id_cell 
    However now what I want to do now is see if the table cell contains a value, for example:

    PHP Code:
    WHERE rs_sub.parent_cell LIKE rs_category.id_cell 
    I'm a PHP newbie and am using Dreamweaver with canned mxp plugins and here is the code I'm trying to get to work:

    PHP Code:
    <?php
    mysql_select_db
    ($database_pdb_conn$pdb_conn);
    $query_rs_sub_cat sprintf("SELECT * FROM pdb_cat"$colname_rs_sub_cat);
    $rs_sub_cat mysql_query($query_rs_sub_cat$pdb_conn) or die(mysql_error());
    $row_rs_sub_cat mysql_fetch_assoc($rs_sub_cat);
    $totalRows_rs_sub_cat mysql_num_rows($rs_sub_cat);

    mysql_select_db($database_pdb_conn$pdb_conn);
    $query_rs_sub_news "SELECT * FROM pdb_sub WHERE url_self LIKE '12345' ";
    $rs_sub_news mysql_query($query_rs_sub_news$pdb_conn) or die(mysql_error());
    $row_rs_sub_news mysql_fetch_assoc($rs_sub_news);
    $totalRows_rs_sub_news mysql_num_rows($rs_sub_news);
    ?>
    <body>

    <?php
      
    do { // horizontal looper
    ?>
    <?php
      
    if ($totalRows_rs_sub_cat>0) {
        
    $nested_query_rs_sub_news str_replace("12345"$row_rs_sub_cat['cat_num'], $query_rs_sub_news);
        
    mysql_select_db($database_pdb_conn);
        
    $rs_sub_news mysql_query($nested_query_rs_sub_news$pdb_conn) or die(mysql_error());
        
    $row_rs_sub_news mysql_fetch_assoc($rs_sub_news);
        
    $totalRows_rs_sub_news mysql_num_rows($rs_sub_news);
        
    $nested_sw false;
        if (isset(
    $row_rs_sub_news) && is_array($row_rs_sub_news)) {
          do { 
    //Nested repeat
    ?>
    <?php
          
    } while ($row_rs_sub_news mysql_fetch_assoc($rs_sub_news)); //Nested move next
        
    }
      }
    ?>
    <?php
        $row_rs_sub_cat 
    mysql_fetch_assoc($rs_sub_cat);
        if (!isset(
    $nested_rs_sub_cat)) {
          
    $nested_rs_sub_cat1;
        }
        if (isset(
    $row_rs_sub_cat) && is_array($row_rs_sub_cat) && $nested_rs_sub_cat++%2==0) {
          echo 
    "</tr><tr>";
        }
      } while (
    $row_rs_sub_cat); //end horizontal looper 
    ?>
    </body>
    Anybody got any ideas how to do this? Thanks.

  2. #2
    SitePoint Enthusiast kaklz's Avatar
    Join Date
    Mar 2004
    Location
    Latvia, Riga
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you have to add wildcard characters for your LIKE command.

    using just LIKE '1234' will return the rows that have the values of '1234' if you want the values that CONTAIN string '1234', then you should use LIKE '%1234%', if you want values that start with '1234', then use '1234%' and so on. Just manipulate with the % wildcard character.

  3. #3
    SitePoint Addict adam2003w's Avatar
    Join Date
    Mar 2004
    Location
    colorado
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! I totally forgot about that stuff. In past use of Like, I saw those "%" characters, but when I manually created my LIKE expression I forgot to include them! Incredible! Thanks.


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
  •