2013-02-16 9 views
0

我有一個MySQL數據庫查詢和顯示在index.php表中。我有一個名爲up.php的文件,它處理在index.php中的查詢輸出的每個表中點擊<a>。我需要一個變量,可以從查詢的每個單獨行中的字段中提取出來,以便單擊<a>時,它將變量傳遞給up.php文件以進行操作。現在變量剛好被寫入,並且等於查詢的最後一行的值,並且數據庫經常更新,所以我不能只爲每行設置一個,它必須是動態的。從數組/ MySQL查詢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 $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文件

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; 
} 
?> 
+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再保留[並正式棄用(https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – j0k 2013-02-16 08:08:59

回答

0

你可以嘗試這樣的。希望可這會幫助你

<?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"); 


$count_variable = 0; 

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'][$count_variable] = $row2['msg']; 
$_SESSION['message2'][$count_variable] = $row3['msg']; 
$_SESSION['mes_id1'][$count_variable] = $row2['mes_id']; 
$_SESSION['mes_id2'][$count_variable] = $row3['mes_id']; 
$_SESSION['totalvotes1'][$count_variable] = $row2['totalvotes']; 
$_SESSION['totalvotes2'][$count_variable] = $row3['totalvotes']; 
$_SESSION['valuess'][$count_variable] = $valuess[1]; 

$count_variable++; 
?> 

<?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']); 
?> 

這將返回一個會話陣列狀波紋管:

陣列( 'MESSAGE1':{ 0: 'mnhj', 1: '建華' }, 'message2':{ 0:'jhg', 1:'jhgjh' } ); etc ...

+0

非常感謝你,我現在就試試! – trickpatty 2013-02-16 07:55:32

+0

是的,你是對的 – Hardik 2013-02-16 07:57:14

+0

我需要在up.php中使用$ count_variable ++,或者我可以通過$ _SESSION ['mes_id1'] [$ count_variable]將值傳遞給up.php嗎? – trickpatty 2013-02-16 08:01:37