Evelyn
Evelyn

Reputation: 656

Check if the number of element is equal to a specific number PHP

From the image below, you can see there is a empty space in second row (red rectangular), by right it should filling up the empty space. This is happening because the first image title is longer than others.

I know by using HTML code, I can use clear: both; and put under the first row to solve it. But my info is read from database. Following is my php and mysql code, I have simplified it, please ignore the method I use to display the image.

enter image description here

$selectSQL="SELECT *, DATE_FORMAT(published_date,'%d %M %Y') AS eventDate FROM media";

while($rows=mysql_fetch_array($selectSQL)){
   <div style="float:left">
     <img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
     <div><?php echo $rows["title"]; ?></div>]
     <div><?php echo $rows["eventDate"]; ?></div>
   </div>
}

I have one solution, which is compare the number of element = 5. Something like the following code. But I'm not sure how to check it the number of element. Please advice or please suggest me if you have another solutions. Thanks!

if($num_element==5){
    echo '<div class="clear"></div>';
}

Upvotes: 0

Views: 53

Answers (2)

Exprator
Exprator

Reputation: 27523

$ i = 0;
while($rows=mysql_fetch_array($selectSQL)){
   <div style="float:left">
     <img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
     <div><?php echo $rows["title"]; ?></div>]
     <div><?php echo $rows["eventDate"]; ?></div>
   </div>
$i++;
if($i % 5 == 0){
 echo '<div class="clear"></div>';
}
}

you can try this code to check the number of items totally displayed and after 5 elements it will print a clear div

Upvotes: 1

Nelson Estuesta Jr
Nelson Estuesta Jr

Reputation: 275

You can use substr() to concat your title when it is very long. you can try it like this:

    while($rows=mysql_fetch_array($selectSQL)){
        <div style="float:left">
          <img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
          <div title="<?php echo $rows["title"];?>">
              <?php if(strlen($rows["title"]) > 6){
                         echo substr($rows["title"],0, 6).'...';
                    }else{
                         echo $rows["title"];
                    } ?>
           </div>
          <div><?php echo $rows["eventDate"]; ?></div>
        </div>
    }

With this code you can customize the length of the words in your title.

Upvotes: 1

Related Questions