How to print contents from a session variable by looping in a foreach statement
Posted
by
itsover9000
on Stack Overflow
See other posts from Stack Overflow
or by itsover9000
Published on 2012-11-28T10:38:28Z
Indexed on
2012/11/28
11:03 UTC
Read the original article
Hit count: 201
im trying to write a code where can print and loop through the contents of my session variable by using a foreach statement
here is my code
<form class="form form-inline" method = "post" action="reportmaker.php">
<select name="rfield">
<option value="">--Select Field--</option>
<?php
$sc2=mysql_query("SELECT * from searchcolumn s left join report_fields r on s.scol_id=r.field_id where s.category != 'wh'");
foreach($sc2 as $sc){
?>
<option value="<?php echo $sc[advsearch_col]; ?>"><?php echo $sc[advsearch_name]; ?></option>
<?php
}
?>
</select>
<button type="submit" value = "submit" id="add" name="add" class="btn pull-right">Add More</button>
</form>
<?php
if(isset($_POST['add']))
{
$_SESSION['temp'][]=$_POST['rfield'];
}
if($_SESSION[temp][]!=""){
foreach($_SESSION[temp][] as $temp)
{
echo $temp;
}
}
?>
the error that appears with this code is
Fatal error: Cannot use [] for reading
the line where the error is is this
if($_SESSION[temp][]!=""){
i need to print the contents of the session array and this is the only way i know how is there a way to fix this?
thanks
=========EDIT
thanks for the answers guys i finally got it
© Stack Overflow or respective owner