PatrickLawler Posted February 16, 2013 Share Posted February 16, 2013 (edited) I have a mySQL database that is queried and displayed in tables in index.php. I have javascript and a file called up.php that handles a click on an <a> with the class "vote" in each table that is output from the query in index.php. I need to have a variable that willl define each MyValue from a field in each seperate row that is queried, so that when the a is clicked it passes the MyValue to the php file to be manipulated. Right now the MyValue 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 im confused on what to do... please help! thanks in advance to anybody who can help me out! Edited February 16, 2013 by PatrickLawler Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/ Share on other sites More sharing options...
trq Posted February 16, 2013 Share Posted February 16, 2013 Sounds like you might need an array but without code, your on your own. Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412744 Share on other sites More sharing options...
PatrickLawler Posted February 16, 2013 Author Share Posted February 16, 2013 I found this on stackflow which seems like it's headed in the right dirrection but I need to be able to access the $values[] dynamically for each table displayed from the query cuz manually typing out $value[0], $value[1] , $value [ 2 ]....... won't work $values[] = $row['id']; and then reference it as $value[0], $value[1] ... Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412745 Share on other sites More sharing options...
trq Posted February 16, 2013 Share Posted February 16, 2013 You need to post some relevant code or a better description of your issue. Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412746 Share on other sites More sharing options...
PatrickLawler Posted February 16, 2013 Author Share Posted February 16, 2013 here is the relevant code from index.php and up.php. I put comments with asterisks in front of them on information i thought was most relevant to solving the problem 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"> <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://localhost:8888/blog1/index.php#<?php echo $noext; ?>" data-counturl="http://localhost:8888/blog1/index.php#<?php echo $noext; ?>" data-text="listen to SD's new podcast! www.sportdebaters.com#<?php echo $noext; ?> @SDebaters " data-related="SDebaters" data-hashtags="SDebaters">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> <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 $valuess[1]; ?>" 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 } ?> 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412754 Share on other sites More sharing options...
PatrickLawler Posted February 16, 2013 Author Share Posted February 16, 2013 (edited) im reading about extract( ) right now..... don't know what I need here.... I haven't been this confused about code in a while..... help ....... and thank you so much for your help so far TRQ! Edited February 16, 2013 by PatrickLawler Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412755 Share on other sites More sharing options...
greenbman Posted February 16, 2013 Share Posted February 16, 2013 My question is how are the sessions even working in index.php when I do not see a session_start(); function on line 1? Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412759 Share on other sites More sharing options...
PatrickLawler Posted February 16, 2013 Author Share Posted February 16, 2013 (edited) sorry i only included part of the index.php file.... session_start(); was declared on line 1...im like 99% sure the rest of it isn't very relevant Edited February 16, 2013 by PatrickLawler Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412761 Share on other sites More sharing options...
Barand Posted February 16, 2013 Share Posted February 16, 2013 (edited) I was confused by the problem description and was totally lost with that triple query/while() loop. But, the usual way to pass information in a link is in a query string <a href="up.php?id=<?php echo $idvalue ?>" ><img src="up.png" alt="Down" /></a> Edited February 16, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274554-diff-php-variable-for-each-field-in-each-db-row/#findComment-1412777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.