连接PHP变量

时间:2018-07-16 14:38:08

标签: php html string-concatenation

我正在尝试在灯箱画廊显示中为图片信息形成字符串。最初我只是使用以下

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

但这是一场噩梦,试图用三元运算符捕获零价格,维度或媒介类型,因此我现在尝试使用IF语句格式化数据并将结果添加到数据数组中。但是,我似乎错过了一些东西,因为结果始终只是“ by”。我的代码是:

        $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++;
}

和表数据语句应简化为

<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 个答案:

答案 0 :(得分:2)

您在代码中使用Si而不是$i。另外,我建议您使用数组而不是动态变量名,这通常是一种不好的做法。

所以:

$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++;
}

然后将其打印为

<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 :(得分:0)

首先,您需要使用以下查询来更新sql查询。

    SELECT t.*, (Select count(*) from test AS t1 where t.artist= t1.artist) AS album_count 
FROM test AS t

我希望这对您有所帮助。