php / Javascript question please help!?

  • Thread starter Thread starter Cazpa
  • Start date Start date
C

Cazpa

Guest
Hi

I am trying to use a javascript that I have got from dynamicdrive and incorporate it into an exsisting website.

I want to take content from a database and then display it with the Javascript but it needs a unique id for the Javascript to work (please see http://www.dynamicdrive.com/dynamicindex1/navigate2.htm

each record on the database has an id [id] and I tried to add it into the javascript <?php echo $row['id'],"\n"; ?> but it only allowed the first one to work, it displayed all the titles fine though just not the drop down thing.

Could someone please help me with the correct way to do this and a sample. many thanks
Thank you "iam"

I have fixed it now, I just done this:

{
$id = $row[id];
?>

<h3 onClick="expandcontent('sc<?php echo "$id"; ?>')" style="cursor:hand; cursor:pointer"><?php echo $row['title'],"\n"; ?></h3>

It seems to work perfect now, many thanks
 
To use php to generate your content IDs you need to have the php do something like this in your html -

<?php // generate your assoc array with your query statement ?>
<?php if ( $row['id'] ) { ?>
do {
<h1 the-js-events-here>xxx</h1>
<p id="<?php echo $row['id']; ?>" class="switchcontent">yyy</p>
<?php while ( $row = fetch_assoc_array($rs...) ) ; ?>

But, to make it clean and not have all that js embedded in your markup, why not use a simple js library like jquery? That would let you easily add the click handler and show/hide functionality unobtrusively. That script from dynamic drive is from 04 - there are better ways of doing what you need to do. JQuery is lightweight, cross browser compatible and easy (and fun) to work with. You would also be able to animate your show/hide elements for nice effects.
 
Back
Top