473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

$_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
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['imgfile'] = array();
  2.  
  3.             do { ?>
  4.               <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
  5.               <?php echo $row_rstImages['image_id']; ?>" /img>
  6.               <br /><a class="<?php echo $row_rstImages['image_title']; ?>" href="displayactual.php" /a> 
  7.               <?php echo $row_rstImages['image_title']; ?>
  8.               <?php $_SESSION['imgfile'][]=$row_rstImages['image_filename']; echo $row_rstImages['image_filename']; var_dump($_SESSION['imgfile']); ?> </li>
  9.               <?php } while ($row_rstImages = mysql_fetch_assoc($rstImages)); }
  10.  
display_results.php

Expand|Select|Wrap|Line Numbers
  1. <?php echo '<img src="img/'.$imgfile.'" width="200" height="150">'; var_dump($_SESSION['imgfile']); ?> </p>
is that the correct way to do it?
Jan 25 '10 #1
1 2513
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <!--
  2. - index.html
  3. -->
  4. <form action="search.php" method="post">
  5.     <input type="text" name="keyword">
  6.     <input type="submit" value="Search">
  7. </form>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * search.php
  4.  */
  5.  
  6. $keyword = mysql_real_escape_string($_POST['keyword']);
  7.  
  8. $sql = "SELECT `id`, `title` FROM `stuff` WHERE `text` LIKE '%{$keyword}%'";
  9. $result = mysql_query($sql) or die(mysql_error());
  10.  
  11. while($row = mysql_fetch_assoc($result)) {
  12.     echo "<a href=\"show_details.php?id={$row['id']}\">{$row['title']}</a><br>";
  13. }
  14. ?>
  15.  
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.
    Expand|Select|Wrap|Line Numbers
    1. // These are wrong:
    2. <img src="" /img>
    3. <a href="" /a>Linkage
    4.  
    5. // This is right for normal HTML
    6. <img src="">
    7. <a href="">Linkage</a>
    8.  
    9. // For XHTML, self-closing tags must be like:
    10. <img src="" />
    11.  
  • The var_dump function exists almost solely for debugging purposes. You aren't going to want to use it in any real output.
Jan 25 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Håvard Olerud Eriksen | last post by:
I've been working on a webshop, and I've got most of the functionality up and running. One problem, however, that I don't seem to be able to solve is as follows. My shopping cart is stored in...
5
by: Rob Tweed | last post by:
Probably a simple question but I can't find the answer anyway. Specifically, is it possible to copy a multidimensional array into the $_SESSION array - ie a deep clone of all keys and data? I...
0
by: Phil Powell | last post by:
What is the most standardized method of utilizing the CURL functions in PHP (version 4.3.2) to be able to retrieve the contents of a remote URL that happens to be dependent upon $_SESSION for its...
4
by: Tom | last post by:
A small problem that has me kinda baffled. This is the situation: I've set up a self-submitting form FORM.PHP. Once the inputs are validated, the info gets put in SESSION variables and the...
5
by: h | last post by:
I am at my wits end; I can not see A SINGLE THING I'm doing wrong. I've read every fricking session related doc. I've searched every fricking group. NO ONE seems to have had the problem I'm having...
2
by: Tom | last post by:
I put together a code that checks for 2 post variables then stores them in a multi-dim $_SESSION array, something like this: If ( isset($_POST && isset($_POST) ) { $_SESSION = $_POST;...
12
by: Michael Windsor | last post by:
I've been trying to integrate some PHP pages of my own with some existing code. The details of this are for the support forums for that code (where I have been asking questions), but I wonder if...
2
by: somecrazyguy | last post by:
Take the following code, one would think that there was absolutely no link between $test and $_SESSION. But if you reload the page, guess what... "After=FAILED". Why? Because for some reason,...
2
by: gm04030276 | last post by:
hey, i'm basically trying to use php sessions and a bit of javascript to make tabbed browsing within a web page using sessions to store the data of the different opened pages. Problem: when i unset...
2
by: sharonDonnelly | last post by:
Hi Really dumb problem that's got me beat. Can someone help. The prolem: I'm trying to count the number of times an item has been clicked. There are many items. I want to create a session...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.