Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Search a database and display results in a table

    I have a script that searches my database and displays the results, but does not display it in a table. I would like the table to be 4 columns, and the rows to continue until however many items there are.

    Here is the script (thanks if you can help!):

    PHP Code:
    <?php
    $db_host 
    "localhost";
    $db_user "username";
    $db_pass "password";
    $db_name "databasename";
    $dbac mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db ($db_name) or die ("Cannot connect to database.");

    if (!
    $_POST['search']){
    ?>
    <form name="Item Search" method="post" action="">
      Item Search:<br>
      <label>
      <input name="swords" type="text" size="30" maxlength="30">
      </label> 
      <br>
      <label>
      <input name="search" type="submit" id="search" value="Search">
      </label>
    </form>
    <?php
    }else{
    $searchwords addslashes(htmlspecialchars($_POST['swords']));
    if (
    strlen($searchwords) < 2){
    echo 
    "Your search must contain at least 3 characters. Please try again.<br><input type=button value='Back' onClick='history.go(-1)'>";
    }else{
    $words explode(' ',$searchwords);
    $totalwords count($words);
    $i 0;
    $searchstring "";
    while (
    $i != $totalwords){
    if (
    $i != and $i != $wordcount){
    $searchstring .= " and ";;
    }
    $searchstring .= "name LIKE '%$words[$i]%'";
    $i $i 1;
    }
    $query mysql_query("SELECT DISTINCT * FROM items where $searchstring");
    if (
    mysql_num_rows($query) == 0){
    echo 
    "No results were found.<br><input type=button value='Back' onClick='history.go(-1)'>";
    }else{
    while (
    $row mysql_fetch_array($query)){
    echo 
    '<table>';
    echo 
    '<tr>';
      echo 
    '<td>'.$row['name'].'</td>';
      echo 
    '<td><img src="'.$row['imgurl'].'"></td>';
      echo 
    '<td>'.$row['category'].'</td>';
      
    // etc
      
    echo '</tr>';
        echo 
    '</table>';

    }
    }
    }
    ?>

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    your echo "<table>" and echo "</table>" need to be outside your while loop, else you are putting each row in a separate table


  •  

    Posting Permissions

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