$_SESSION array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LongWei
    New Member
    • Dec 2009
    • 6

    $_SESSION array.

    Hi all,

    i am trying to do a img search application using PHP and mysql. Basically it goes like this, on the index.php a user will key in some keywords to search and it will return results on the image search

    Then i will try store the array of results in a $_SESSION.

    index.php
    Code:
    $_SESSION['imgfile'] = array();
    
    			do { ?>
                  <li><img src="img/<?php echo $row_rstImages['image_filename']; ?>" alt="<?php echo $row_rstImages['image_title']; ?>" name="search" width="200" height="150" id="search
    			  <?php echo $row_rstImages['image_id']; ?>" /img>
                  <br /><a class="<?php echo $row_rstImages['image_title']; ?>" href="displayactual.php" /a> 
    			  <?php echo $row_rstImages['image_title']; ?>
                  <?php $_SESSION['imgfile'][]=$row_rstImages['image_filename']; echo $row_rstImages['image_filename']; var_dump($_SESSION['imgfile']); ?> </li>
                  <?php } while ($row_rstImages = mysql_fetch_assoc($rstImages)); }
    display_results .php

    Code:
    <?php echo '<img src="img/'.$imgfile.'" width="200" height="150">'; var_dump($_SESSION['imgfile']); ?> </p>
    is that the correct way to do it?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    No. That's not the correct way to do it.

    Typically you don't need sessions for stuff like this. You just put a HTML form on your index page, have it submit to the search page, where the results are displayed.

    For example:
    [code=php]<!--
    - index.html
    -->
    <form action="search. php" method="post">
    <input type="text" name="keyword">
    <input type="submit" value="Search">
    </form>[/code]
    [code=php]<?php
    /*
    * search.php
    */

    $keyword = mysql_real_esca pe_string($_POS T['keyword']);

    $sql = "SELECT `id`, `title` FROM `stuff` WHERE `text` LIKE '%{$keyword}%'" ;
    $result = mysql_query($sq l) or die(mysql_error ());

    while($row = mysql_fetch_ass oc($result)) {
    echo "<a href=\"show_det ails.php?id={$r ow['id']}\">{$row['title']}</a><br>";
    }
    ?>
    [/code]
    This is obviously not an image search, but you get the point.

    A few things to consider:
    • During the first iteration of a do ... while loop, the variables inside the loop are set as they were before the loop. That is: the statement in the while condition is not executed until after the first iteration. So if you are planing to go through the result set of a mysql_query, you are going to want to use a normal while loop.
    • Your HTML is all messed up.
      [code=html]
      // These are wrong:
      <img src="" /img>
      <a href="" /a>Linkage

      // This is right for normal HTML
      <img src="">
      <a href="">Linkage </a>

      // For XHTML, self-closing tags must be like:
      <img src="" />
      [/code]
    • The var_dump function exists almost solely for debugging purposes. You aren't going to want to use it in any real output.

    Comment

    Working...