Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi all,

Please see the following:

<form action="snippets_1a.php" method="post" name="snippets_1a" >

 

<input name="snippet_ref" type="hidden" value="' . $snippet_info['snippet_ref'] . '" />

 

<input name="submitted" type="hidden" value="TRUE" />

 

<input name="submit" type="submit" value="Request this snippet" />

 

</form>

</div>

';

}

 

echo'

</table>

<br />';

 

//If a user has requested a snippet place it in a session

if (isset($_POST['submitted'])) //Has the form been submitted?

{

$snippet_ref = $_POST['snippet_ref'];

 

echo'

<p>The snippet requested is ref. ' . $snippet_ref . '</p>

';

 

$_SESSION['requested_snippet'] = $snippet_ref;

print_r($_SESSION);

}

 

The form is generated from a database query and there will be many of them on a single page, each with a different 'snippet_ref'. That bit works OK. The user can then click on any of the buttons. If they do so the code above is meant to take the relevant 'snippet_ref' from the form and add it to a session. The idea being that the page will reload, the user can then click another button with a different 'snippet_ref' and this will be added to the session. A bit like adding stuff to a basket. The echo is there just to check that each button calls the correct 'snippet_ref' and it does. The problem is that the session is just overwritting the last request with the new one instead of adding it on.

 

My feeling is that it has something to do with the page reloading and just creating a new 'snippet_ref' with key zero.

 

Thanks for any help.

Cheers

Paul

Link to comment
Share on other sites

Edward,

Thanks for the quick reponse. I tried what you suggested by doing the following:

 

if (isset($_POST['submitted'])) //Has the form been submitted?

{

$snippet_ref = $_POST['snippet_ref'];

 

$snippet_requested = array();

 

$snippet_requested[] = $snippet_ref;

echo'

<p>The snippet requested is ref. ' . $snippet_ref . '</p>

';

 

$_SESSION[$snippet_requested];

print_r($_SESSION);

}

 

When I run it it comes up with an illegal offset error.

 

Paul

Link to comment
Share on other sites

Hi all,

 

I think the problem may lie with me not explaining myself properly. Please take a look at the following screen dump.

 

 

This was created with the following code:

 

$q = "SELECT *

FROM snippets

WHERE (display = 'y') AND (price <= 8)

ORDER BY price

LIMIT 12

";

 

$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

//check that a match was made with the return of 1 row of data

//Assign the results to an array

if (mysqli_num_rows($r) >= 1)

{

$snippets_1 = array();

 

while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))

{

$snippets_1[] = $row;

}

 

mysqli_free_result($r);

}

else

{

echo'

<p>There are no snippets in this price range</p>

';

}

 

//Create a table for the results

echo'

<h2>Snippet info</h2>

<table width="780px">

';

 

//Take the array of snippet info and display in a block in a grid

//Include a form and button in order for the user to request a snippet

foreach($snippets_1 as $snippet_info)

{

echo'

<div class="each_snippet">

<img src="images/snippets/' . $snippet_info['image'].'.jpg" width="180px" height="180px" ><br/>

<a href="#" id="openModal">View larger image....</a>

<div id="modal">

<div id="modalMask">

</div>

<div id="modalContent">

<img src="images/snippets/' . $snippet_info['image'].'.jpg" width="500px" height="500px" >

<input type="button" id="closeModal" value="Close">

</div>

</div>

<p>'. $snippet_info['price'] . '</p>

<p>'. $snippet_info['name'] . '</p>

<p>'. $snippet_info['description'] . '</p>

<form action="snippets_1a.php" method="post" name="snippets_1a" >

 

<input name="snippet_ref" type="hidden" value="' . $snippet_info['snippet_ref'] . '" />

 

<input name="submitted" type="hidden" value="TRUE" />

 

<input name="submit" type="submit" value="Request this snippet" />

 

</form>

</div>

';

}

 

echo'

</table>

<br />';

 

The idea is that a user can click on a button and it'll add the 'snippet_ref' to a session. They can then click on a second button and it'll add that 'snippet_ref' to the session and so on. I attempted to do this with the following code:

 

$snippet_requested = array();

 

$_SESSION['snippets_requested'] = $snippet_requested;

 

 

//If a user has requested a snippet place it in a session

if (isset($_POST['submitted'])) //Has the form been submitted?

{

$snippet_ref = $_POST['snippet_ref'];

 

 

 

 

 

}

 

$_SESSION['snippets_requested'][] = $snippet_ref;

 

echo'<pre>';

print_r($_SESSION);

'</pre>';

 

?>

 

The problem being that the session keeps getting overwritten and all that changes is the value of first key, as follows:

 

Array

(

[snippets_requested] => Array

(

[0] => 8

)

 

)

 

I hope that makes more sense. Any help would be great. Would Ajax work better in this situation? If so I hope it's not the only solution, I've not learnt about Ajax yet!!

 

Cheers

Paul

Link to comment
Share on other sites

Your problem is here:

$_SESSION['snippets_requested'] = $snippet_requested;

You keep assigning each new value to the same session index. Instead, you want to create an array of values:

$_SESSION['snippets_requested'][] = $snippet_requested;

  • Upvote 1
Link to comment
Share on other sites

Larry,

Thanks for the quick response but I was just about to post that I've sorted it with the following code:

 

if (isset($_POST['submitted'])) //Has the form been submitted?

{

if(isset($_SESSION['snippets_requested']))

{

$snippet_ref = $_POST['snippet_ref'];

 

$_SESSION['snippets_requested'][] = $snippet_ref;

}

else

{

$snippet_requested = array();

 

$_SESSION['snippets_requested'] = $snippet_requested;

 

$snippet_ref = $_POST['snippet_ref'];

 

$_SESSION['snippets_requested'][] = $snippet_ref;

}

}

 

I figured that I was creating a new session each time the user clicked on a button, so it was always going to stay on the first key. So I've added a check to see if it is the first time or subsequent times.

 

I'm sorry if I wasted anyones time with this.

 

Cheers

Paul

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...