Jump to content

How Do I Count An Array?


ricky spires

Recommended Posts

hello.

 

How do i count how many pics in this array?

 

$aUnits = array('pic1.jpg => '1', 'pic2.jpg => '2', 'pic3.jpg => '3');

 

i tried this but its not working

 

echo count($aUnits);

 

 

thanks

rick

 

You were missing some quotation marks.

 

$aUnits = array('pic1.jpg' => '1', 'pic2.jpg' => '2', 'pic3.jpg' => '3');

Link to comment
Share on other sites

true.. but im my actuall code they are not missing

 

this is the code:

 

<?
$sThumbTemplate = <<<HTML
<li><a href="#" rel="nofollow" title="{title}"><img src="{fileurl}" alt="{title}" /></a></li>
HTML;
$sImageTemplate = <<<HTML
<div class="sliderkit-panel">
  <img src="{fileurl}" alt="{title}" />
  <div class="sliderkit-panel-textbox">
   <div class="sliderkit-panel-text">
	   <div class="arrow-left"><a rel="nofollow" href="#" title="Next line"></a></div>
  <p>{IMGno}/7</p>
  <div class="arrow-right"><a rel="nofollow" href="#" title="Next photo"></a></div>
   </div>
   <div class="sliderkit-panel-overlay"></div>
  </div>
</div>
HTML;
$sThumbs = $sImages = '';
$sFolder = 'assets/img/slider/';
$aUnits = array(
  'pic1.jpg' => '1', 'pic2.jpg' => '2', 'pic3.jpg' => '3', 'pic4.jpg' => '4',
  'pic5.jpg' => '5', 'pic6.jpg' => '6', 'pic7.jpg' => '7'
);
foreach ($aUnits as $sFileName => $sTitle) {
  $sThumbs .= strtr($sThumbTemplate, array('{fileurl}' => $sFolder . 't_' . $sFileName, '{title}' => $sTitle));
  $sImages .= strtr($sImageTemplate, array('{fileurl}' => $sFolder . $sFileName, '{title}' => $sTitle, '{IMGno}' => $sTitle, '{id}' => $sID));
}
header("Content-Type: application/json");
require_once('Services_JSON.php');
$oJson = new Services_JSON();
echo $oJson->encode(array('thumbs' => $sThumbs, 'images' => $sImages));
?>

 

 

if you look for

<p>{IMGno}/7</p>

at the top. i want to replace the 7 with the number of pics in the array

Link to comment
Share on other sites

You could add a new placeholder into $sImageTemplate, such as {IMGcount}.

 

Then get the number of items in the $aUnits array using count.

$iUnitsCount = count($aUnits);

 

Then replace the placeholder by adding it into the array for strtr.

$sImages .= strtr($sImageTemplate, array(..., '{IMGcount}' => $iUnitsCount));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.