div height() from jQuery to PHP variable

Hello Experts!

I want to use jQuery's height() function to get the height from hundreds of divs.

For example: I have a page showing several articles like this:

<div class="article">
<?php echo $article1; ?>
</div>

<div class="article">
<?php echo $article2; ?>
</div>

<div class="article">
<?php echo $article3; ?>
</div>

etc

I need to find out the div height for these different div boxes (the same class). And then I need to put these values into a PHP variable, make some calculations for each div (different height's) and then show the page to the user. And the user should see nothing more than a regular web page.

Is that even possible?
LVL 2
AphXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Barry JonesCommented:
If you use AJAX, then once the DOM is ready, get the size of the DIVs and post it to the server to retrieve the content back, and populate the DIVs?
0
AphXAuthor Commented:
Alright. That should work. But, what about the div classes, they all have got the same class name. Do you think that will be a problem?

I'm not that into AJAX. I'm not able to write all the code from scratch. Do you know any tutorial or something where they're doing something like this?
0
gr8gonzoConsultantCommented:
The order of processing means that this will be impossible to do all in the same HTTP request. Basically, you'll need to:

1. Request #1: Have the DIVs render fully.
2. Request #1: use jQuery(document).ready() to run a function that loops through the DIVs and collects their heights. Putting them into a JSON array would probably work best for this.
3. Request #1: Use jQuery to send the JSON data to the PHP script.
4. Request #2: PHP accepts the JSON data, decodes it, and renders the final page.

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Barry JonesCommented:
Aphx, to be honest while there are worse things you can natively code in javascript than ajax calls, I would save yourself some work and use a framework such as Prototype.js or JQuery.  These frameworks make life much easier in my opinion.

If the DIVs have the same classname, thats not a problem as you are only concerning yourself with the dimensions, and can easily get an array of the DIVs by className, but best to give each DIV a unique ID as well - makes life easier when the ajax call is complete and you need to refer to a specific DIV.

If you let me know whether you are happy to use a JS framework, then I will mock up some sample code (HTML/JS/PHP) for you later tonight...
0
AphXAuthor Commented:
Thank you so much! I really appreciate your help!

Yes, I'm happy to use a JS framework. What technique or framework I need to use doesn't matter at all.
0
Barry JonesCommented:
Ok, give me a few hours and Ill post the code.

One question: why do you need the PHP script to know the height of the DIVs?  Is it to be able to truncate the contents?
0
AphXAuthor Commented:
Thank you very much!

I need the div height because I'm building a site with a complicated design.

Gradient boxes with rounded corners and drop shadows, inner glows and so on. That´s easy if the boxes are static, but on boxes with dynamical height's it´s a little harder.

Everything is done except the gradient. What I'm going to do:

If box1's height is 110px, I will echo 110 div boxes inside box1 with different colors that will create the gradient color inside the box. I also need the height of the div to calculate the right RGB values for each gradient div.

I really appreciate your helpfulness!
0
Barry JonesCommented:
No problem..

110 DIVs 1px high sounds scary!  Not sure browsers will appreciate that too much though...  Might slow things down somewhat.  I cant imagine IE6 handling that too well.. :) Can you not generate a gradient in PHP using the GD2 library perhaps? (http://forums.devshed.com/php-development-5/create-gradient-image-with-gd-php-536986.html)

How are the article DIVs created?  Surely they are coded into the html page?  What I am getting at, is if you know the heights of the DIVs before the page loads, then you can of course do your gradient processing in the PHP section at the top of the page, which would negate the need for any ajax calls at all?  Or perhaps I missed something.,.?
0
AphXAuthor Commented:
Actually it will output ~110 DIVs times how many article DIVs :D So it will be about 3000 DIVs. I'm not developing for IE6. Seems to work fine in other browsers :) Assuming that the user doesn't use a computer from the 90´s :)

About the GD2 library, it looks interesting, but the same problem is still there though, that I would need the DIV heights for all the article DIVs to make the gradient dynamic to the DIV height.

The article DIVs is echoed out from a database. The users can write articles so the first article DIV can be 30px and the second one can be up to 200px etc.

So the browser doesn't know the div height until the page is loaded =/ That´s the prob
0
Barry JonesCommented:
Ah ok gotcha.  Pls bear with me - I am not going to get time tonight.  I will definitely get back to you within 48 hours though ok?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AphXAuthor Commented:
Dude, I appreciate what you're doing so much! I'm so glad that you're willing to help me with this!

I don´t have the skills to do this by my self.

I'm looking forward to hear from you! :)
0
hieloCommented:
save as hielo.html and compute.php and try it
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
 
$(function(){
	var params="";
	$(".article").each(function(index)
	{
		params+="&div["+index+"]="+$(this).height();
	}
	);
 
 
$.ajax({
   type: "POST",
   url: "compute.php",
   data: params.substr(1),
   success: function(msg){
     alert( "Response:\n" + msg );
	
	var d=msg.split("^");
	var i=0;
	while(d.length){
		$(".article:eq("+i+")").html( d.shift() );
	}
   }
 });
 
});
</script>
<div class="article">
<?php echo $article1; ?>a
</div>
 
<div class="article">
<?php echo $article2; ?>b
</div>
 
<div class="article">
<?php echo $article3; ?>c
</div>
 
</body>
</html>
 
 
compute.php
<?php
if( isset($_POST['div']) ){
	foreach($_POST['div'] as $k =$v)
	{
		echo "Value Received for div {$k} = $v^";
	}
}
?>

Open in new window

0
Barry JonesCommented:
There you go - hielo beat me to it :)  

His solution looks good..
0
hieloCommented:
actually,
...
        var i=0;
        while(d.length){
                $(".article:eq("+i+")").html( d.shift() );
        }
...
 
 
should be:
...
        var i=0;
        while(d.length){
                $(".article:eq("+i+")").html( d.shift() );
                ++i;
        }
...

Open in new window

0
AphXAuthor Commented:
Guys, you are my heroes! I really wish that I had more points to assign on this one!

TheFoot, you showed a big engagement and you were fully helpful from start! I really appreciate this!

hielo, you made me a code snippet that works 100% like I wanted to!

Thank you again!

I am so happy about this!

I hope I can help you guys with something in the future!

Peace!

/Joel
0
Barry JonesCommented:
Thanks for the points!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.