SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php and javascript: how to pass a variable from php to javascript?

    Hi all,

    i would like to display a certain number of pictures in a html table row.
    The number of images to display is relative and depending of the number of pictures that can be displayed on the screen width: for that, i have to know the value of the screen width. How?
    With a javascript script, i got the value of the screen width, but i don't know how to use this value into a php code. Any help would be very appreciated. This is my code:

    Code:
    	<body>	
    		<script language='JavaScript'>
    			var name="LargEcran";
    			lLargEcran=window.screen.width;
    		</script>	
    		<?php
    			include "connexion.php";			
    			
    			$largEcran = $_GET['largEcran'];
    				
    			$php_select_pays=$_POST['select_pays'];			
    			
    			$requete="select repertoire, nom_timbre from timbres where pays like '$php_select_pays' ";
    			$resultat=mysql_query($requete);
    			
    			echo "<table><tr><td><h1>Brésil</h1></td></tr></table>";
    			
    			while($ligne=mysql_fetch_array($resultat))
    			{
    				echo '<img src="../images/'.$ligne['repertoire'].'/'.$ligne['nom_timbre'].'" alt="" title="" />';
    			}
    		?>
    
    	</body>

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well tonquim PHP is parsed on the server while JavaScript is parsed client side so the fact is PHP parse before the JavaScript. So one approach you could possible take is to display X amount of images and assign them IDs and use CSS to make their display none, then using JavaScript make however many images you actually want to display set to display: block or something.
    Kayzio - We don't hesitate, we accelerate.

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you to kayzio for his answer.
    I found the command line to pass a javascript variable value to a php one:
    $valeurphp = "<script language='Javascript'> document.write(window.screen.width); </script>";
    I don't understand how css can dynamicaly manage the display of the image in the way i'm expecting to do. In fact i'm expecting to display the pictures without the display of the horizontall scroll bar.

    If i could get the width of each cell, and sum them, and then compare the sum width the width of the screen, i could solve my problem.

    NB: the images doesn't have the same width.

    Any better idea would be very appreciated.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um, why don't you just float the images to the left with CSS?
    That way as many as possible will fit, and then they'll go to a new line. You don't even need to know the width of the images.
    Code HTML:
    <style type="text/css">
    #imageContainer img {float: left;}
    </style>
     
    <div id="imageContainer">
    <img /> <img /> <img />
    </div>
    mikehealy.com.au
    diigital.com art, design . Latest WorkSaturday Morning


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •