开发者

Checking the value of all a loops value

I have this loop in PHP that echoes out a table of results,

<table cellspacing="0" cellpadding="3">
  <tr>
    <td><b>Content Image Title</b></td>
    <td><b>Content Image Type</b></td>
    <td><b>Headline Image</b></td>
    <td><b>Content Image Belongs To</b></td>
    <td><b>Date Created</b></td>
    <!--<td><b>Uploaded By</b></td>-->

  </tr>
  <?php $colours = array("#f9f9f9", "#f3f3f3"); $count = 0;?>
  <?php foreach ($allContentImages as $contentImages) : ?>
    <tr bgcolor="<?php echo $colours[$count++ % count($colours)];?>">
      <td><?php echo "<a href='#' class='screenshot' rel='/media/uploads/$contentImages[categoryId]/$contentImages[contentImageName]'>".$contentImages['contentImageName']; ?></td>
      <td><?php echo $contentImages['contentImageType']; ?></td>
      <td><?php if($contentImages['isHeadlineImage'] == 1){ echo "Y";}else{echo "N";} ?></td>
      <td><?php echo $contentImages['contentTitle'] ?></td>
      <td><?php echo date("d-m-Y", $contentImages['contentImageDateUploaded']); ?></td>
      <td align="left"><a class="delete" href="<?php echo base_url();?>dashboard/deleteContentImage/<?php echo $contentImages['contentImageId'];?>"><img src="/media/images/icons/cancel.png" alt="Delete A Category"/></a></td>
    </tr>
  <?php
    if($contentImages['isHeadlineImage'] == '0') {
        echo "<tr bgcolor='red'>";
        echo "<td><p>You need to assign a headline image</p></td>";
        echo "</tr>";

  ?>
  <?php endforeach; ?>
</table>

I need to check for each content piece with th开发者_如何学运维e same title that there is headline image, and if not then echo a new row that is red...but all I get is a new row every time there is an image that is not a headline image. Can anyone help me? I don't mind using javascript if that helps to match the values of the td's? But obviously my attempt is not correct.


It sounds like what you want to do has to be precalculated, since the foreach loop can not know about future content.

Maybe something like this before your foreach statement:

$contentHasHeadlineImage = array();
foreach ($allContentImages as $contentImages) {
  if ( $contentImages['isHeadlineImage'] == 1)
    $contentHasHeadlineImage[ $contentImages['contentTitle'] ] = true;
}

And then you can use

if (array_key_exists($contentImages['contentTitle'], $contentHasHeadlineImage)) {
  // Has headline image... 
}

to to verify if a certain title has a headline.


Try my code below:

<table cellspacing="0" cellpadding="3">
  <tr>
    <td><b>Content Image Title</b></td>
    <td><b>Content Image Type</b></td>
    <td><b>Headline Image</b></td>
    <td><b>Content Image Belongs To</b></td>
    <td><b>Date Created</b></td>
    <td>Action</td>
  </tr>
  <?php
  $colours = array("#f9f9f9", "#f3f3f3");
  $num_colours = count($colours);
  $i = 0;
  ?>
  <?php foreach ($allContentImages as $row) : ?>
    <tr bgcolor="<?php echo $colours[($i++) % $num_colours]; ?>">
      <td><a href="#" class="screenshot"
             rel="/media/uploads/<?php echo $row['categoryId']; ?>/<?php echo $row['contentImageName']; ?>">
        <?php echo $row['contentImageName']; ?>
      </a></td>
      <td><?php echo $row['contentImageType']; ?></td>
      <td><?php echo $row['isHeadlineImage'] == 1 ? "Y" : "N"; ?></td>
      <td><?php echo $row['contentTitle']; ?></td>
      <td><?php echo date("d-m-Y", $row['contentImageDateUploaded']); ?></td>
      <td align="left"><a class="delete"
                          href="<?php echo base_url(); ?>dashboard/deleteContentImage/<?php echo $row['contentImageId'];?>">
        <img src="/media/images/icons/cancel.png" alt="Delete A Category" />
      </a></td>
    </tr>
    <?php if ( $row['isHeadlineImage'] == 0 ): ?>
      <tr bgcolor="red">
        <td colspan="6"><p>You need to assign a headline image</p></td>
      </tr>
    <?php endif; ?>
  <?php endforeach; ?>
</table>

While tidying your code, I found an unclosed a href in the first column. For the special extra row, I add colspan. I assume $row['isHeadlineImage'] have value only 1 or 0 (integer or boolean).

I also found out that you access array key without using quote. It can become potential bug in the future.

Write the code in tidy indentation and consistent when using block or echoing the php variable will help you found the bug quickly. Also, inspect the result in browser using Firebug in firefox or Web Inspector in Safari and Google Chrome to see if the page structure is like you want to have, all the tag is balanced and closed in right place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜