Jump to content

PHP and Javascript


Akenatehm

Recommended Posts

Hey Guys,

 

I have a little script here running. The javascript part is a rollover that changes the CSS class on hover of the <div> element. The div element and its contents are given from a PHP loop which displays news entries inside the main div.

 

The Javscript is:

<script type="text/javascript">
function mouseOver()
{
document.getElementById('entry').className = "hoverentry";
}
function mouseOut()
{
document.getElementById('entry').className = "normalentry";
}
</script>

 

The PHP is:

$connect = mysql_connect($host,$user,$pass) or die(mysql_error());
$selectdatabase = mysql_select_db($db) or die(mysql_error());

$getentries = mysql_query("SELECT * FROM newsentries ORDER BY id DESC ");

while($filterentries = mysql_fetch_assoc($getentries))
{
$title = $filterentries['title'];
$content = $filterentries['content'];
$author = $filterentries['author'];
$date = $filterentries['date'];

echo '<div id="entry" class="normalentry" onMouseOver="mouseOver()" onMouseOut="mouseOut()">';
echo '<h3 class="newsentry">' . "$title  " . "</h3>";
echo '<p class="newsdate">' . "$date"  . "</p> <br />";
echo '<p class="newsentry">' .  "$content" . '</p>';
echo '<p class="newsposter">Posted By ' . "$author" . "</p> </div>";
}

 

and the CSS is:(I dont think you need it but just in case.)

div.normalentry{
margin-top: 10px;
margin-bottom: 10px;
background-color: #2a2a2a;
background-image: url('scanline.png');
width:570px;
}

div.hoverentry{
margin-top: 10px;
margin-bottom: 10px;
background-color: #111111;
background-image: url('scanline.png');
width:570px;
}

h3.newsentry{
color: white;
font-size:24;
padding:15px 10px 2px 10px;
font-family: sans-serif;
}
p.newsdate{
color:white;
font-size:12;
display: inline;
padding: 3px 0px 0px 25px;
}
p.newsentry{
color: #959292;
padding: 15px 10px 10px 10px;
}
p.newsposter{
color:white;
font-size: 14;
padding-left: 12px;
padding-top: 5px;
padding-bottom: 15px;
}

 

When I hover over any of the news entries, the same one div at the top changes (in its rollover effect). What I want, is a way for each one to change its own, and not eachothers.

 

Help would be appreciated.

Link to comment
Share on other sites

You're assigning all the div's the same ID!!  They need to be different ;D

 

<script type="text/javascript">
function mouseOver(id)
{
document.getElementById('entry'+id).className = "hoverentry";
}
function mouseOut(id)
{
document.getElementById('entry'+id).className = "normalentry";
}
</script>

<?php
$i = 0;
while($filterentries = mysql_fetch_assoc($getentries))
{
$i++
$title = $filterentries['title'];
$content = $filterentries['content'];
$author = $filterentries['author'];
$date = $filterentries['date'];

echo '<div id="entry'+$i+'" class="normalentry" onMouseOver="mouseOver('+$i+')" onMouseOut="mouseOut('+$i+')">';
echo '<h3 class="newsentry">' . "$title  " . "</h3>";
echo '<p class="newsdate">' . "$date"  . "</p> <br />";
echo '<p class="newsentry">' .  "$content" . '</p>';
echo '<p class="newsposter">Posted By ' . "$author" . "</p> </div>";
}

 

But, a better thing to do would be just to handle this in the CSS using pseudo:

div.normalentry{
margin-top: 10px;
margin-bottom: 10px;
background-color: #2a2a2a;
background-image: url('scanline.png');
width:570px;
}

div.normalentry:hover{
margin-top: 10px;
margin-bottom: 10px;
background-color: #111111;
background-image: url('scanline.png');
width:570px;
}

 

And please, try and format your code before posting on here  :) Other than that, let me know if either of these solutions work for you. The CSS one is the way to go IMHO. Also, remember that ID's on the same page are unique identifiers, and should only be used once on a single page.

 

Link to comment
Share on other sites

Try this (if they all have the same ID still):

#entry.normalentry{
margin-top: 10px;
margin-bottom: 10px;
background-color: #2a2a2a;
background-image: url('scanline.png');
width:570px;
}

#entry.normalentry:hover{
margin-top: 10px;
margin-bottom: 10px;
background-color: #111111;
background-image: url('scanline.png');
width:570px;
}

 

If this doesn't work, then still try playing with the CSS. You may need to have the first ID outside of #entry before the .normalentry.......

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.