drvpv7995 2018-07-16 14:38 采纳率: 100%
浏览 80
已采纳

连接PHP变量

I am trying to form a string for the picture info in a lightbox gallery display. Originally I just used the following

<td><div style='height:175px;'></div></td><td><a href='images/memwork/".$cell1['folder']."/".$cell1['filename']."' data-lightbox='image-".$cell1['id']."' data-title='".$cell1['title']." by  ".$cell1['artist']."<br />".$cell1['medium']." &nbsp; &nbsp; ".$cell1['width']." x ".$cell1['height']." cm.  <br />Price: £".$cell1['priceu']."'><img class='tn' src='images/memwork/".$cell1['folder']."/".$cell1['tn']."' /></a></td>

But this became a nightmare trying to trap zero prices, dimensions or medai types with ternary operators so I am now trying to format the data with IF statements and add the result into the data array. However, I seem to have missed something as the result is always just " by ". My code is:

        $query = "SELECT * FROM gallery WHERE sig=1 LIMIT $start,6";
}
$result = mysqli_query($db, $query);
$i=1;
while($row = mysqli_fetch_assoc($result)) {
    ${'cell'.$i} = $row;
    $gal_artist = $row['artist'];
    $query = "SELECT COUNT(*) AS num FROM gallery WHERE artist='$gal_artist'";
    $total = mysqli_fetch_array(mysqli_query($db,$query));
    ${'cell'.$i}['num_pics'] = $total['num'];

    $picdata = ${'cell'.Si}['title']." by ".${'cell'.Si}['artist'];
    if (${'cell'.Si}['medium'].${'cell'.Si}['width'] !='') $picdata = $picdata."<br />";    
    if (${'cell'.Si}['width'] >0) $picdata = $picdata." &nbsp; &nbsp; ".${'cell'.Si}['medium']." &nbsp; &nbsp; ".${'cell'.Si}['width']." x ".${'cell'.Si}['height']." cm.";
    if (${'cell'.Si}['price'] >0) $picdata = $picdata."<br />Price: £".${'cell'.Si}['price'];
    ${'cell'.Si}['picdata'] = $picdata;

$i++;
}

and the table data statements should simplify to

<td><div style='height:175px;'></div></td><td><a href='images/memwork/".$cell1['folder']."/".$cell1['filename']."' data-lightbox='image-".$cell1['id']."' data-title='".$cell1['picdata']."'><img class='tn' src='images/memwork/".$cell1['folder']."/".$cell1['tn']."' /></a></td>
  • 写回答

2条回答 默认 最新

  • droe9376 2018-07-16 14:50
    关注

    You're using Si instead of $i in your code. In addition, I would suggest you to use array instead of dynamic variable name, which is usually a bad practice.

    So :

    $result = mysqli_query($db, $query);
    $i=1;
    while($row = mysqli_fetch_assoc($result)) {
        $cells[$i] = $row;
        $gal_artist = $row['artist'];
        $query = "SELECT COUNT(*) AS num FROM gallery WHERE artist='$gal_artist'";
        $total = mysqli_fetch_array(mysqli_query($db,$query));
        $cells[$i]['num_pics'] = $total['num'];
    
        $picdata = $cells[$i]['title']." by ".$cells[$i]['artist'];
        if ($cells[$i]['medium'].$cells[$i]['width'] !='') $picdata = $picdata."<br />";    
        if ($cells[$i]['width'] >0) $picdata = $picdata." &nbsp; &nbsp; ".$cells[$i]['medium']." &nbsp; &nbsp; ".$cells[$i]['width']." x ".$cells[$i]['height']." cm.";
        if ($cells[$i]['price'] >0) $picdata = $picdata."<br />Price: £".$cells[$i]['price'];
        $cells[$i]['picdata'] = $picdata;
    
    $i++;
    }
    

    Then print it like

    <td><div style='height:175px;'></div></td><td><a href='images/memwork/".$cells[1]['folder']."/".$cells[1]['filename']."' data-lightbox='image-".$cells[1]['id']."' data-title='".$cells[1]['picdata']."'><img class='tn' src='images/memwork/".$cells[1]['folder']."/".$cells[1]['tn']."' /></a></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line