body onload=test($number); problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
beautnic136
Forum Newbie
Posts: 4
Joined: Sun Nov 02, 2008 10:46 pm

body onload=test($number); problem

Post by beautnic136 »

hi , im new here and what i would like to do is to count the number of id image that i have into my table upload2 and with the as num from the select count, creating dynamic div in javascript its working perfectly when i do it with a button click but its not working with the body onload plz help me to resolve this error



also i would like to put this newdiv as an array so with a next button i can change to the next div
and the name of "div" i would like to do div + 1 but its not working atm a help would be appreciate too

last thing i would like to do is to put the path of the image from my upload2 table in each div with a for a while loop

ty to help me hope its clear ? if not well tell me :p

if u can at least help me out with the onload thing i will be very happy

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
<style type="text/css"> 
div.test
{ 
 background-color:#B9CDED; 
 width: 100px; 
 text-align: center; 
 height: 50px; 
} 
</style> 
 
 
 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
 
newDiv = new Array();
 
function test(t) 
{
 alert(t);
 
 for (i=0;i<t;i++) 
    {
      newdiv = document.createElement('div');  
      newdiv.className = 'test'; 
      document.body.appendChild(newdiv); 
    }
}
-->
</SCRIPT>
 
 
</head>
 
<body onload="test($number);">
<?php
 
$connection = mysql_connect("localhost","root","") or die("impossible de se connecter au serveur mysql.<p>Veuillez réessayer plus tard");
$res = mysql_select_db("ilssontcons") or die("impossible de sélectionner la base de données <b>ecole</b>.<p>Veuillez réessayer plus tard");
 
$resultat = mysql_query("SELECT COUNT(*) AS num FROM upload2");
$result = mysql_fetch_array($resultat);
 
$number = $result['num'];
 
print "<input type='button' name='button' value='button' onClick=test('$number')>";
?>
</body>
 
</html>
 
 

this code is working atm with the path thing

but i would like to put it in the div that u saw in the other code

Code: Select all

 
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<?php
$array = array();
 
 
$connection = mysql_connect("localhost","root","") or die("impossible de se connecter au serveur mysql.<p>Veuillez réessayer plus tard");
$res = mysql_select_db("upload2") or die("impossible de sélectionner la base de données <b>ecole</b>.<p>Veuillez réessayer plus tard");
 
$requeteStr = "SELECT path FROM upload2";
$resultatRequete = mysql_query($requeteStr) or die("la requete de sélection a échoué.<p>Veuillez réessayer plus tard");
$result=mysql_query($requeteStr);
 
 
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
} 
else
{
    while ($row = mysql_fetch_array($result)) 
     {
      $array[$row['path']] = $row["path"];
    
?>
    <img height="300"  src=<?=$array[$row['path']];?>>
<?php       
     
    }
}
 
?>
</body>
</html>
 
 
well i hope i will get some help and ty for the help:)

p.s. im studying this at school, so im a bit noob to it

at the end i will add from my database some text into the div also so all these divs with a next button will show the next news all this dynamically
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: body onload=test($number); problem

Post by kaszu »

$number is a php variable, so

Code: Select all

<body onload="test($number);">
should be

Code: Select all

<body onload="test(<?php echo $number; ?>);">
Sorry, but I didn't understood what you are trying to achieve next. You want a list of images (with titles) in a divs (1 div visible at the time) and using a button switch a visible div? Or something else?
beautnic136
Forum Newbie
Posts: 4
Joined: Sun Nov 02, 2008 10:46 pm

Re: body onload=test($number); problem

Post by beautnic136 »

hi kaszu, ty for the reply and for the big help its working now

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
<style type="text/css"> 
div.test
{ 
 background-color:#B9CDED; 
 width: 100px; 
 text-align: center; 
 height: 50px; 
} 
</style> 
 
 
 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
 
newDiv = new Array();
 
function test(t) 
{
 
 /*for ( i = 0; i < 3; i++ )
   TabIds[i] = new Array('nouvelle'+i);*/
 for (i=0;i<t;i++) 
    {
      newdiv = document.createElement('div');  
      newdiv.className = 'test'; 
      document.body.appendChild(newdiv); 
    }
}
-->
</SCRIPT>
 
<?php
$connection = mysql_connect("localhost","root","") or die("impossible de se connecter au serveur mysql.<p>Veuillez réessayer plus tard");
$res = mysql_select_db("ilssontcons") or die("impossible de sélectionner la base de données <b>ecole</b>.<p>Veuillez réessayer plus tard");
 
$resultat = mysql_query("SELECT COUNT(*) AS num FROM upload2");
$result = mysql_fetch_array($resultat);
 
$number = $result['num'];
?>
 
 
 
</head>
<body onLoad="test(<? echo $number; ?>)"> 
 
</body>
 
</html>
 
what i would like to do is to count the number of image text i got from my database and retrieve them and with a count as num, this will create dynamic div for each image ( path ) and text and that will be some news that will change up every second

i know i can do this just with javascript but this is a school project and i need it to do that way


the first thing i would like to know is how to put this as an array

Code: Select all

 
div.test
{ 
 background-color:#B9CDED; 
 width: 100px; 
 text-align: center; 
 height: 50px; 
} 
 
 
 
*for ( i = 0; i < 3; i++ )
   TabIds[i] = new Array('nouvelle'+i);*/
 for (i=0;i<t;i++) 
    {
      newdiv = document.createElement('div');  
      newdiv.className = 'test'; 
      document.body.appendChild(newdiv); 
    }
 
i try to figure out myself but cant atm ty for the help

is it clear what i want to do ?

i just want with a select count to create div first and put the image path ( that is from database also ) in the div and the text in the div.


i want to put it in an array so after with a ( next button ) i can push it to the next news or with a play button. i can settimeout it.. i know how to do this already.


i just have trouble with

Code: Select all

 
div.test
{ 
 background-color:#B9CDED; 
 width: 100px; 
 text-align: center; 
 height: 50px; 
} 
 
 
 
/*for ( i = 0; i < 3; i++ )
   TabIds[i] = new Array('nouvelle'+i);*/
 for (i=0;i<t;i++) 
    {
      newdiv = document.createElement('div');  
      newdiv.className = 'test'; 
      document.body.appendChild(newdiv); 
    }
 

p.s.
/*for ( i = 0; i < 3; i++ )
TabIds = new Array('nouvelle'+i);*/ this is a commentary as example

ty to take your time to help me
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: body onload=test($number); problem

Post by kaszu »

Create all divs with php, then hide all except 1 with javascript.

Code: Select all

<div id="listContainer">
<?php
$sql = 'SELECT * FROM upload2';
//...
while ($row = mysql_fetch_array($result))
{
    //Create div with image and title
    echo '<div><img src="' . addslashes($row['path']) . '" alt="" /><p>' . htmlentties(...Where does title comes from?...) . '</p></div>';
}
?></div>
<script type="text/javascript">
//Get all divs
var divList = document.getElementById('listContainer').getElementsByTagName('div');
//Hide all but first
for(var i=1,j=divList.length; i<j; i++) {
    divList[i].style.display = 'none';
}
</script>
To create divs with javascript you need to encode data (which is retrieved from database) as JSON and then itterate through it and create divs, but it's a worse solution since if javascript will be disabled, they won't be created.
beautnic136
Forum Newbie
Posts: 4
Joined: Sun Nov 02, 2008 10:46 pm

Re: body onload=test($number); problem

Post by beautnic136 »

hey kaszu ty for ur time man

well if its a wrong solution how to do it

can u tell me a good way on how i should do it ? i really need to do the news with php since its a school project

i dont want the answer but just an idea on how to concept it ? i will find out the rest by myself

im new to php and javascript thats why im asking for help and ty for ur time again i really appreciate it:)

i still have 5 week to have the right answer so its ok:)
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: body onload=test($number); problem

Post by kaszu »

As I wrote, output all news with php and with javascript hide all except 1, then in javascript when needed (timer or button press) hide the one which is visible and show next one.
Progressive Enhancement with JavaScript
beautnic136
Forum Newbie
Posts: 4
Joined: Sun Nov 02, 2008 10:46 pm

Re: body onload=test($number); problem

Post by beautnic136 »

hi:)

i was so tired last night at 12 am that i didnt understand it at first but now,i do:)

gg 12 am lol... :rofl: i should sleep more sometimes :wink:

p.s. the text for the news come from my table upload2 and its a varchar(100) field which is a string

i have done a search about htmlentities function and there is the definition The htmlentities function takes a string and returns the same string with HTML converted into HTML entities.

so if i understand it right, when i retrieve my text from my table upload2, this function will check if the field i retrieve is a string and if yes it will show it in a safety way ?

example :

Code: Select all

 
PHP Code:
// An imaginary article submission from a bad user
//  it will redirect anyone to example.com if the code is run in a browser
$userInput = "I am going to hax0r your site, hahaha!
    <script type='text/javascript'>
    window.location = 'http://www.example.com/'
    </script>'";
    
//Lets make it safer before we use it
$userInputEntities = htmlentities($userInput);
 
//Now we can display it
echo $userInputEntities;
 
 
ty again for this big help !!! but im gonna finish it by myself
ty again:)
Post Reply