Search code examples
phphtmlstring-concatenation

concatenate PHP variables


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>

Solution

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