简体   繁体   中英

Session start in Joomla using a while loop

I have 2 databases, one with Joomla installed and the other with my personal data.

I need to work contemporaneously with these 2 databases.

Now I do a while into my DB with this query:

 $query="Select * From lista where user_id='$id'"; 
 $result = mysql_query($query) or die(mysql_error());
 while($row=mysql_fetch_array($result))
 {
     $ID_STRUCTURE=$row['ID'];
     ECHO '<P>Modifica <a href="..../test/index.php/modifica">'.$row['struttura'].'</P>';
     $session =& JFactory::getSession();
     $session->set( 'myvar', $ID_STRUCTURE );
 }

When I go on the page /test/index.php/modifica I use this code:

  $session =& JFactory::getSession();
  $myvar= $session->get('myvar');
  echo $myvar;

I must obtain only the $ID_STRUCTURE of the correspondent $row['struttura'] .

If I use an array in the while and a foreach in the other page /test/index.php/modifica , I obtain all the values.

How can I fix it?

Don't use the while loop directly. Store it into an array or variable, so you can use it over and over. Now it's returning you the last value because the while loop already ran. So when you call it again, its already at its last record.

$query="Select * FROM `lista` WHERE `user_id` = '$id'"; 
$result = mysql_query($query) or die(mysql_error());
$a_Array = array();
while($row=mysql_fetch_array($result))
{
    $a_Array['id'] = $row['ID'];
    $a_Array['link'] = '<P>Modifica <a href="..../test/index.php/modifica">'.$row['struttura'].'</P>';
}
$session =& JFactory::getSession();
$session->set( 'myvar', $a_Array);

NOW USE A foreach() loop to get all the data on the other page. In that array are the values $myvar['id'] and $myvar['link'].

foreach($myvar as $value){
echo $value['id'].$value['link'];
}

Of course, because you are always overwrite that in your loop. You need to create an array, or make the key to be unique.

while ($row = mysqli_fetch_array($result)) {
    $ID_STRUCTURE = $row['ID'];
    ECHO '<P>Modifica <a href="..../test/index.php/modifica">' . $row['struttura'] . '</P>';
    $myvarArray[] = $ID_STRUCTURE;
}
$session = & JFactory::getSession();
$session->set('myvar', $myvarArray);

Use mysqli or PDO instead mysql functions since they are deprecated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM