Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello folks.
This block of code creates a thumbnail for an individual item in an online shop. This code is duplicated if an item is added to the shop. The item amount could be anything from 1 to 100, depending on how many items are for sale.
What I would like to do is count the amount of times the id="thumb" appears in the html, then echo this amount, for example - 'Number of items: # '

HTML
<div id="thumb">
<div id="thumbbackground">
<a href="../screen-printing/map.php">
<div id="image" class="a"><img src="../screen-printing/thumbnails/map.jpg" width="170" height="170"/></div>
</a>
<div id="prelo1" class="p"></div>
</div>
<div id="thumbname"><p class="desc8">Map</p></div>
</div>
Posted
Updated 20-Aug-12 4:57am
v4

1 solution

PHP
$text = '<div id="thumb"><img /></div><div id="thumb"><img /></div>';
$countMe = 'id="thumb"';
$numOccurances = substr_count($text, $countMe);
 
Share this answer
 
v2
Comments
ridoy 20-Aug-12 13:39pm    
Though i'm not so familiar with php but it seems to me the logic is right here..so my +5
enhzflep 20-Aug-12 13:58pm    
Thanks!
Jimtiger 20-Aug-12 14:40pm    
Thanks for your quick response enhzflep, the logics here indeed ridoy (but) I haven't yet managed to implement it correctly(I'm new to php).
I echoed out $numOccurances count and noticed its count is from whats inside $text =
I would like to count the entire web page/or a portion of it where the thumbs are present. Possible?
I tried wrapping all the 'id="thumb"' inside '$text =' for counting but obviously the thumbs would no longer be visible. How would you tackle this one?
enhzflep 20-Aug-12 15:05pm    
You're welcome Jimtiger.
Just for clarity, where is the file/content you'd like to examine/count?
Is it:
On someone else's server?
On the same server as the php page?
In the php page?
Jimtiger 20-Aug-12 22:18pm    
The file(shop-all.php) is on my server (testing on localhost)
Content to be counted is in file shop-all.php. I hope this helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900