in_array problem/question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JasonWilliams
    Senior Member
    • Jul 2004
    • 117
    • 3.6.x

    in_array problem/question

    Hey all,

    A tad stuck on something I'm attempting to do with an existing script.

    I have an autocomplete script which is searching within 3 rows, I'm trying to make it so that when the search happens, depending what row it's in, it will display something slightly different.

    Here's the code I have (this isn't the complete code):
    PHP Code:
    <?php
    if (!mysql_num_rows($getRecord)) {        
            echo 
    "Sorry, I ain't got that!"
    } else {
        while (
    $row mysql_fetch_array($getRecord)) {
            if (
    in_array($searchq$row["title"])) { echo "Title works"; echo '</ul>'; exit; }
            elseif (
    in_array($searchq$row["album"])) { echo "Album works"; echo '</ul>'; exit; }
            elseif (
    in_array($searchq$row["artist"])) { echo "Artist works"; echo '</ul>'; exit; }
        
    ?>
            <li><a href="playlist.php?search=<?php echo $row['title']; ?>&dcat=title">Title: <?php echo $row['title']; ?></a><small><a href="playlist.php?search=<?php echo $row['artist']; ?>&dcat=artist">Artist:  <?php echo $row['artist']; ?></a><a href="playlist.php?search=<?php echo $row['album']; ?>&dcat=album">Album: <?php echo $row['album']; ?></a></small></li>
        <?php 
         

        }
    It's the in_array I'm having issues with, I need to check within either of three rows, title, artist or album. The code above doesn't work ... where am I going wrong? As far as I was aware they are arrays at that point.

    Thanks,

    Jason
  • Yves R.
    vBulletin QA
    • Nov 2003
    • 3861
    • 5.6.X

    #2
    $row['title'], $row['album'] or $row['artist'] are simple values, $row is an array.

    in_array() works like this:

    PHP Code:
    in_array('term to search''array where you searching'); 
    You could try with:

    PHP Code:
    in_array($searchq, array($row['title'])) 
    Do the same for album & artist.

    vBulletin QA - vBulletin Support French - Lead Project Tools developer

    Next release? Soon(tm)

    Comment

    • JasonWilliams
      Senior Member
      • Jul 2004
      • 117
      • 3.6.x

      #3
      Thanks for the info, for the past few hours I've been trying different things trying to understand my mistakes, I've tried what you've suggested but it doesn't appear to work, here's the code as it is now:

      PHP Code:
      <?php if (!mysql_num_rows($getRecord)) {        
              echo 
      "Sorry, I ain't got that!"
      } else {
          while (
      $row mysql_fetch_array($getRecord)) {
              if (
      in_array($searchq, array($row['title']))) { echo "Title works"; echo '</ul>'; exit; }
              elseif (
      in_array($searchq, array($row['album']))) { echo "Album works"; echo '</ul>'; exit; }
              elseif (
      in_array($searchq, array($row['artist']))) { echo "Artist works"; echo '</ul>'; exit; }
          
      /*?>
              <li><a href="playlist.php?search=<?php echo $row['title']; ?>&dcat=title">Title: <?php echo $row['title']; ?></a><small><a href="playlist.php?search=<?php echo $row['artist']; ?>&dcat=artist">Artist:  <?php echo $row['artist']; ?></a><a href="playlist.php?search=<?php echo $row['album']; ?>&dcat=album">Album: <?php echo $row['album']; ?></a></small></li>
          <?php */
           

          }
      Any further suggestions or ideas? I'm starting to understand in_array much clearer now, it's just how to make it so when the data is from a particualar row, it formats it one certain way.

      Thanks again,

      Jason

      Comment

      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
      Working...