【问题标题】:iteration within a while loop在while循环内迭代
【发布时间】:2011-09-02 20:43:51
【问题描述】:

很抱歉问了两次,但另一个已关闭,因为它缺少重要信息,我很抱歉,但是我的代码是这样的:

if (isset($_GET['id'])) {
// Connect to the MySQL database  
include "connect.php"; 
$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
// Use this var to check to see if this ID exists, if yes then get the bio info 
$sql = mysql_query("SELECT * FROM bio\n"
. "LEFT JOIN bio_media \n"
. "ON bio.id=bio_media.bioid\n"
. "WHERE bio.id = ".$id."\n"
. "\n"
. " LIMIT 0, 30 ");
    // get all the bio details
    while($row = mysql_fetch_array($sql)){ 
         $name = $row["name"];
         $age = $row["age"];
         $division = $row["division"];
         $pic = $row["pic"];
         $story = $row["story"];
         $fb = $row["fb"];
         $tw = $row["tw"];
         $type = $row['type'];
         $file = $row['file'];
         $alt = $row['alt'];
         $title = $row['title'];
         $Age= DetermineAgeFromDOB ($age);
         while ($type == "image"){
                $images .='<a rel="lb[pp_gal]" href="images/'.$file.'" ><img width="60px" alt="'.$alt.'" title="'.$title.'" src="images/'.$file.'" /></a>';     
                break;
                }
        $i=-1;
        if($type == "video"){
            $i++;               
            $videos .='<li><a href="watch.php?id='.$id.'&v='.$file.'&ref='.$i.'?iframe=true&width=745&height=520" rel="lb" title="'.$alt.'">'.$title.'</a></li>';
            }

问题:如何获取 url 的“ref”部分以更新数据库中每个条目的 +1?

【问题讨论】:

  • url 的 ref 部分不是迭代它只是去 0
  • 用这个代替preg_replace():if ( ! ctype_digit($_GET['id'])) die('Invalid input'); else $id = (int) $_GET['id'];
  • ...您将 $i 设置为 -1,然后将其递增一次 - 在这种情况下它将始终为 0,是的。
  • webbiedave,它显示在我的,但代码是: if($type == "video"){ $i++; $videos .='
  • '.$title.'
  • '; } 对于所有出现的条目,它显示 ref=0 它只添加到 $i 一次
  • $i=-1;
  • 标签: php while-loop iteration


    【解决方案1】:

    如果我理解正确,您必须在循环中去掉$i=-1;,而只需将$i=0; 放在循环之前。

    顺便说一句,你每次都在循环中覆盖你的变量;要么将$row 添加到数组并使用它,要么根本不使用这些临时变量,直接在需要的地方使用$row

    【讨论】:

    • 如果我使用 $i=0,那么所有的 ref=1。我需要它来计算每一个。就 $row 而言,您能说得更具体一点吗?
    • @Symply 不,您需要在 while 循环(外部循环)之前将 $i 设置为 0。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签