Click here to Skip to main content
14,921,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
So basicly what i am working on is project were a user can upload images from the admin panel.
The path to those images gets saved in the database and the image itself is saved in a folder called images.
In some other file i call the table that has the path to the images and i display those images with a loop.
The loop creates a div for every 3(this number can change is not important) images displayed.
What i want is to change that number if the number of div tags is equal to 4 divs.

How can i achieve that?

How can i get the number of div tags in html file and then pass that number to php for some other calculations?
Remember all of this has to be done in the same file.
Example:

<div>
<img>
<img>
<img>
</div>

<div>
<img>
<img>
<img>
</div>

<div>
<img>
<img>
<img>
</div>

<div>
<img>
<img>
<img>
</div>


What I have tried:

Now that i have 4 div tags i want to change the number of images displayed to something different.
Posted
Updated 21-Jun-19 7:57am
v2

Is it going to run on client side?
If so, you can use jQuery to do this very easily.
JavaScript
$('div').length
will give you the count of divs.

To see it in action right here, you can:
1. Pres F12 (get the web browser console open in most browsers)
2. copy the line of code above.
3. paste into the browser console
4. press <enter>

I get 195 for this (CP) page.

Here's a snapshot of what I see in the console output:
https://i.stack.imgur.com/poGrV.png[^]

Pure JavaScript
If you don't want to use jQuery or it's not available you can use pure JS code:
JavaScript
document.getElementsByTagName('div').length


Again, you can test that here in the console window of your browser.
   
v2
Comments
simple world 20-Jun-19 14:58pm
   
Thank you for replying but how can i take that number and pass it to php?
Actually that is my main problem.
raddevus 20-Jun-19 15:42pm
   
Send it in as a querystring variable, then extract it.
build your URL that you will submit:
www.yourthing.com/thing/?divCount=195

<?php
echo $_GET['divCount']; //Output: 195
?>



Your next question will be: HOw do I post the URL?
Check out this --> https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php
simple world 20-Jun-19 16:38pm
   
But there is a problem.
I want all of this to be done in the same page.
I will update my question so that you can understand better.
I have found a solution to my problem it had to do with how the templates css was displaying the images.
I was build with the flex property instead of bootstrap but i figured it out.
Thank you for the help.
   

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