Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a mySQL database that is queried and displayed in tables in index.php. I have a file called up.php that handles a click on an < a > in each table that is output from the query in index.php. I need to have a variable that I can extract from a field in each seperate row that is queried, so that when the < a > is clicked it passes the variable to the up.php file to be manipulated. Right now the variable is just being over written and is equal to the value of the last row queried and the database is updated frequently, so I can't just set one to each row, it has to be dynamic.

Here is the index.php file

<?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
}
?>

and here is the up.php file

up.php

<?php
session_start();
include("config.php");

$message1 = $_SESSION['message1'];
$message2 = $_SESSION['message2'];
$mes_id1 = $_SESSION['mes_id1'];
$mes_id2 = $_SESSION['mes_id2'];
$totalvotes1 = $_SESSION['totalvotes1'];
$totalvotes2 = $_SESSION['totalvotes2'];
$ip=$_SERVER['REMOTE_ADDR'];

$ip_sql=mysql_query("select ip_add from Voting_IP where mes_id_fk='$mes_id1' and ip_add='$ip'");
$count=mysql_num_rows($ip_sql);
$ip_sql2=mysql_query("select ip_add from Voting_IP where mes_id_fk='$mes_id2' and ip_add='$ip'");
$count2=mysql_num_rows($ip_sql2);
//********* testing if these variables are being passed.....
echo $mes_id1;
echo $mes_id2;
$valuess[0] = $_SESSION['valuess'];
echo $valuess[0];
//********
// if the user has already voted, execute script
if($count==0 && $count2!=0)
{
$sql = "update Messages set totalvotes=totalvotes+1  where mes_id='$mes_id1'";
mysql_query( $sql);
$sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$mes_id1','$ip')";
mysql_query( $sql_in);
$sql = "update Messages set totalvotes=totalvotes-1  where mes_id='$mes_id2'";
mysql_query( $sql);
$sql_in = "DELETE FROM Voting_IP WHERE mes_id_fk='$mes_id2'";
mysql_query( $sql_in);
// if the user has not voted, execute script
}
else if($count==0 && count2==0)
{
$sql = "update Messages set totalvotes=totalvotes+1  where mes_id='$mes_id1'";
mysql_query( $sql);
$sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$mes_id1','$ip')";
mysql_query( $sql_in);
echo $mes_id1;
echo $mes_id2;
}
?>

Thank you so much to anybody who is able to help me!

$(".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);
 }

 });


  }




});

  });
share|improve this question
 
The session variables are overwritten each iteration of your while loop. Instead, change <a href="up.php" to a query URL, ie <a href="up.php?key1=value1&key2=value2", etc. And in up.php, get it like $_GET['key1']. –  Jon Feb 17 '13 at 3:06
 
thank you so much! so, I could do this but I also have a version of this script where the href is blank and I use javascript/ajax with a class on the anchor to load up.php and is displays right away in index.php.... so if I were to use that script to make it look more like a web app, is there another way to do this? if not I'll just resort to this method here which seems easy enough... but If there is another way to accomplish this I'd appreciate the feedback. again, thank you so much –  Patrick Lawler Feb 17 '13 at 3:13
 
You can pass them to up.php from your javascript as well. But sessions, how you have them, will only contain the last row each and every time - you need to pass the data that you want to use to your script. Otherwise, I'm not sure I understand the question. –  Jon Feb 17 '13 at 3:17
 
don't know how I'd do it with javascript... ive been stuck on this for like 6 days now... im over it... just gonna use your method cuz I know it will work and I need to move on with the project. ill just have to have to style the up.php page and make it flow with my project. thanks Jon! –  Patrick Lawler Feb 17 '13 at 3:23
 
Post your javascript, I'll help you figure out how to do it in there with what you have. ^^ –  Jon Feb 17 '13 at 3:24
show 6 more comments

1 Answer

up vote 0 down vote accepted

With the javascript, I can see what you need to do in order to get it working how you want.

Instead of putting the key1=value1 etc attached to the href, put it in its own attribute called data-options, without the ? pre-pending it. So looking like:

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

From there, you would modify the JS to be:

$(".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);

From there, you would access them in the up.php and down.php files by using $_POST['key1'], and only need to echo out what your JS is expecting in return from its AJAX call. ^^

share|improve this answer
 
omg. I love you bro. no homo –  Patrick Lawler Feb 17 '13 at 9:13
 
wish I could repay you somehow. and btw, thanks for defending our country –  Patrick Lawler Feb 17 '13 at 9:22
 
Haha no problem at all. ^^ Glad that you were able to get it working how you want. And no reason to feel the need to 'repay', SO is about helping out other programmers. =] (And you're welcome? Never sure how to respond to that haha) –  Jon Feb 17 '13 at 9:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.