php从数组/查询中提取并按行定义php会话变量,而不覆盖

php从数组/查询中提取并按行定义php会话变量,而不覆盖,php,session,extract,Php,Session,Extract,我有一个mySQL数据库,可以在index.php的表中查询和显示。我有一个名为up.php的文件,用于处理index.php中查询输出的每个表中的。我需要有一个变量,可以从查询的每个单独行的字段中提取,这样当单击时,它会将变量传递到up.php文件进行操作。现在这个变量刚刚被重写,它等于查询的最后一行的值,并且数据库经常更新,所以我不能只为每一行设置一个,它必须是动态的 这是index.php文件 <?php $sql = mysql_query("SELECT * FROM blogD

我有一个mySQL数据库,可以在index.php的表中查询和显示。我有一个名为up.php的文件,用于处理index.php中查询输出的每个表中的。我需要有一个变量,可以从查询的每个单独行的字段中提取,这样当单击时,它会将变量传递到up.php文件进行操作。现在这个变量刚刚被重写,它等于查询的最后一行的值,并且数据库经常更新,所以我不能只为每一行设置一个,它必须是动态的

这是index.php文件

<?php
$sql = mysql_query("SELECT * FROM blogData ORDER BY id DESC");
//query for even numbered rows where mes_id = even
$sql2=mysql_query("SELECT * FROM messages WHERE mod(mes_id,2) = 0 ORDER BY mes_id DESC");
//query for odd numbered rows where mes_id = even
$sql3=mysql_query("SELECT * FROM messages WHERE mod(mes_id,2) = 1 ORDER BY mes_id DESC");

while(($row = mysql_fetch_array($sql))AND($row2 = mysql_fetch_array($sql2))AND($row3 = mysql_fetch_array($sql3)) ){
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
$podcast = $row['podcast'];
$datetime = $row['datetime'];


$message1=$row2['msg'];

//******* this is the variable from the query that needs to be held and not overwritten ********
$mes_id1=$row2['mes_id'];

$totalvotes1=$row2['totalvotes'];

$message2=$row3['msg'];

//******* this is the second variable from the query that needs to also be held and not overwritten *******
$mes_id2=$row3['mes_id'];

$totalvotes2=$row3['totalvotes'];

//attempting to implement this array...? not sure how to use it correctly...
$valuess[]=$row2['mes_id'];

//******* I was trying to use these session variables in up.php but they were being overwritten in the query ********
$_SESSION['message1'] = $row2['msg'];
$_SESSION['message2'] = $row3['msg'];
$_SESSION['mes_id1'] = $row2['mes_id'];
$_SESSION['mes_id2'] = $row3['mes_id'];
$_SESSION['totalvotes1'] = $row2['totalvotes'];
$_SESSION['totalvotes2'] = $row3['totalvotes'];
$_SESSION['valuess'] = $valuess[1];
?>

<?php
// variable used to display file name of $podcast without the extension
$noext = $podcast;
$echodub = rawurlencode($podcast);
// code to display $noext without the file extension
$info = pathinfo($noext);
$noext_name =  basename($noext,'.'.$info['extension']);
?>

<!--  ********* echo php variables in html format, in a table with the class of "podcast" -->
<table class="podcast" border="1">
<tr>
<td class="title">
 <?php echo $title; ?>
</td>
<td class="timeandcategory">
 <?php echo $datetime; ?>  <br>
 <?php echo $category; ?>
 <?php echo $_SESSION['mes_id1']; ?>
 <?php echo $_SESSION['mes_id2']; ?>
 <?php echo session_id(); ?>
</td>
</tr>
<tr>
<td class="content">
 <?php echo $content; ?>
</td>
<td class="myfblike">


<span class='st_fblike_large' displayText='Facebook Like'></span><br>
 <span class='st_facebook_large' displayText='Facebook'></span><br>
 <span class='st_twitterfollow_large' displayText='Twitter Follow'></span><br>
 <span class='st_pinterest_large' displayText='Pinterest'></span><br>
 <span class='st_email_large' displayText='Email'></span><br>
 <span class='st_sharethis_large' displayText='ShareThis'></span><br>

</td>

</tr>
<tr>
<td class="audio">
 <!--echo the audio file -->
  <ul class="playlist">
   <li><a href="<?php echo"uploads/$podcast"; ?>"><?php echo"$noext_name"; ?></a></li>

  </ul>

</td>
<td>

 <!-- ********** this is the cell in the table where the veriables need to be held and sent to up.php ******** -->

<div id="main">
<div id="left">
<span class='up'><a href="up.php" class="" id="<?php echo $_SESSION['mes_id']; ?>" name="up"><img src="up.png" alt="Down" /></a></span><br />
<?php echo $_SESSION['totalvotes1'] ?><br />
</div>
<div id="message">
<?php echo $_SESSION['message1'] ?>
</div>
<div class="clearfix"></div>
</div>
//********the down.php file is the same as the up.php file... just with opposite variables... im not concerned with this yet until i get the variables to display correctly in up.php
<div id="main">
<div id="left">
<br />
<?php echo $_SESSION['totalvotes2'] ?><br />
<span class='down'><a href="down.php" class="" id="<?php echo $_SESSION['mes_id2']; ?>" name="down"><img src="down.png" alt="Down" /></a></span>
</div>
<div id="message">
<?php echo $_SESSION['message2'] ?>
</div>
<div class="clearfix"></div>
</div>

</td>
</tr>

</table>


<br>
<?php
}
?>

有了javascript,我可以看到您需要做什么才能让它按照您的意愿工作

不要将
key1=value1
等附加到
href
中,而是将其放在自己的名为
数据选项的属性中,而不使用
预挂起它。看起来像:

<a href="" class="vote" name="up" data-options="key1=value1&key2=value2">Up/Down</a>

从那里,您可以使用
$\u POST['key1']
在up.php和down.php文件中访问它们,并且只需要回显JS在AJAX调用中所期望的结果^^

会话变量在
循环的每次迭代中都会被覆盖。相反,请更改
非常感谢!因此,我可以这样做,但我也有一个版本的脚本,其中href为空,我使用javascript/ajax和锚上的一个类来加载.php,并立即在index.php中显示。。。。所以,如果我使用这个脚本使它看起来更像一个web应用程序,有没有其他方法可以做到这一点?如果不是的话,我就用这个方法,看起来很简单。。。但如果有其他方法来实现这一点,我将感谢您的反馈。再次感谢您,您可以将它们从javascript传递到up.php。但是会话(您如何拥有它们)每次都只包含最后一行—您需要将要使用的数据传递给脚本。否则,我不确定我是否理解这个问题。我不知道如何使用javascript。。。我已经在这上面呆了6天了。。。我结束了。。。我只想用你的方法,因为我知道它会起作用,我需要继续这个项目。我只需要设计up.php页面的样式,并使其与我的项目保持一致。谢谢你,乔恩!发布您的javascript,我将帮助您了解如何使用您现有的内容在那里实现它^^但愿我能报答你。顺便说一句,谢谢你保卫我们的国家哈哈,没问题。^^很高兴你能让它按你想要的方式工作。没有理由感到需要“回报”,帮助其他程序员也是如此(不客气?永远不知道该如何回应哈哈)
$(".vote").click(function() 
{

var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);


if(name=='up')
{

$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "up.php",
data: dataString,
cache: false,

success: function(html)
{
parent.html(html);

}  });

}
else
{

$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "down.php",
data: dataString,
cache: false,

success: function(html)
{
   parent.html(html);
 }

 });


  }




});

  });
<a href="" class="vote" name="up" data-options="key1=value1&key2=value2">Up/Down</a>
$(".vote").click(function() 
{

var id = $(this).attr("id");
var name = $(this).attr("name");
var eData = $(this).attr("data-options");
var dataString = 'id='+ id + '&' + eData ;
var parent = $(this);