简体   繁体   English

使用while循环在Joomla中开始会话

[英]Session start in Joomla using a while loop

I have 2 databases, one with Joomla installed and the other with my personal data. 我有2个数据库,一个数据库安装了Joomla,另一个数据库则存储了我的个人数据。

I need to work contemporaneously with these 2 databases. 我需要同时使用这两个数据库。

Now I do a while into my DB with this query: 现在,我使用此查询对数据库进行了一段while

 $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: 当我进入/test/index.php/modifica页面/test/index.php/modifica我使用以下代码:

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

I must obtain only the $ID_STRUCTURE of the correspondent $row['struttura'] . 我必须只得到$ID_STRUCTURE记者的$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. 如果我在while使用数组,在另一页/test/index.php/modifica使用foreach ,则将获取所有值。

How can I fix it? 我该如何解决?

Don't use the while loop directly. 不要直接使用while循环。 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. 现在,它返回您的最后一个值,因为while循环已经运行。 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. 现在使用一个foreach()循环来获取另一页上的所有数据。 In that array are the values $myvar['id'] and $myvar['link']. 在该数组中是值$ myvar ['id']和$ 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. 由于不建议使用mysqli或PDO而不是mysql函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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