Change Array Format

Discussion in 'PHP' started by adamjblakey, Dec 10, 2010.

  1. #1
    Hi,

    I have an ajax script with currently uses an array to search through, however i want to change this so instead of looping through an array it searched through a table in a database.

    How would i do this to the below:

    
    <?php
    
    $q = '';  if (isset($_GET['q'])) {      
    $q = strtolower($_GET['q']);  }  
    if (!$q) {      return;  }    
      
    $items = array(      
    "Science Grade 2 with Henry Lecture" => "",      
    "Maths Grade 6 with Henry Lecture" => "",      
    "Social Studies Grade 2 with Henry Lecture" => "",      
    "World War 2 Battle" => ""  );    
    
    foreach ($items as $key => $value) {          
    if (strpos(strtolower($key), $q) !== false) {                  
    echo "$key|$value\n";          
    }  
    }  
    
    ?>
    
    PHP:
     
    adamjblakey, Dec 10, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Change these values with your own : table_name,column_to_check,column_to_echo

    
    $items = array(      
    "Science Grade 2 with Henry Lecture" => "",      
    "Maths Grade 6 with Henry Lecture" => "",      
    "Social Studies Grade 2 with Henry Lecture" => "",      
    "World War 2 Battle" => ""  );
    
    $result = mysql_query("SELECT * FROM table_name WHERE column_to_check IN (".implode(",",$items).")");
    if(mysql_num_rows($result) > 0)
    {
    while($row = mysql_fetch_array($result))
    {
    echo $row['column_to_echo'].'<br />';
    }
    } else {
    echo 'No results found !';
    }
    
    PHP:
     
    tvoodoo, Dec 10, 2010 IP
  3. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Oh... miss-read your post...
    change this :
    "SELECT * FROM table_name WHERE column_to_check IN (".implode(",",$items).")"
    to
    "SELECT * FROM table_name WHERE column_to_check = '".$_GET['q']."'"
     
    tvoodoo, Dec 10, 2010 IP