Solved

loop a javascript element with a counter for array value in php

Posted on 2011-03-10
5
322 Views
Last Modified: 2012-08-14
I have added a news scroller that uses javascript from http://www.dynamicdrive.com/dynamicindex2/crosstick.htm

What I want to do is take news items from the MySQL database and add it to the array for the script

 
<script type="text/javascript">

/*Example message arrays for the two demo scrollers*/

var pausecontent2=new Array()

pausecontent2[0]='<a href="http://www.mynews.com"><?php echo $row_newsScroller['news_date']; ?> - <?php echo $row_newsScroller['news_title']; ?></a>'


</script>

Open in new window


I need a loop to get all the items in the news table, and then a loop to get the numbers.

I am kinda confused how to go about it, also I have created the SQL query to order by DESC

Can anyone help me to do this?

Thank you
0
Comment
Question by:cataleptic_state
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Author Comment

by:cataleptic_state
ID: 35095101
Currently all I have is this
 
<script type="text/javascript">

/*Example message arrays for the two demo scrollers*/

var pausecontent2=new Array()

<?php do { ?>
pausecontent2[0]='<a href="http://www.ansarmustafa.com/slikhaar"><?php echo $row_newsScroller['news_date']; ?> - <?php echo $row_newsScroller['news_title']; ?></a>'
<?php } while ($row_newsScroller = mysql_fetch_assoc($newsScroller)); ?>

</script>

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35095190
Basically you need to create a javascript array with PHP so use something along these lines

UNTESTED

<script type="text/javascript">

/*Example message arrays for the two demo scrollers*/

<?php
     $yourQuery = "select ...... etc   ... ";

     $jsText = "";
     $jsLink = "";

     $rs = mysql_query( $yourQuery );
     if ( $rs ) {

           while ( $rw = mysql_fetch_assoc( $rs ) ) {
                $jsText .=  ",'" . htmlspecialchars( $rw['yourTextField'], ENT_QUOTES ) . "'";
                $jsLink .=  ", 'http://". $_SERVER['HTTP_HOST'] . "/" . $rw['yourPageId'] . "'";
           }

     }

     echo "var titles = new Array( $jsText );\n";
     echo "var links = new Array( $jsLink );\n";
?>

// more javascript here
</script>

Open in new window


Then it is only a matter of writing the javascript to access the array of titles and the array of link URLs. You will need to adjust the URL format in the above code because I don't know what format you are using.

That should get you started.

;-)
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 35095216
Try:
<script type="text/javascript">

/*Example message arrays for the two demo scrollers*/

var pausecontent2=new Array()

<?php do { ?>
	pausecontent2[pausecontent2.length]='<a href="http://www.ansarmustafa.com/slikhaar"><?php echo $row_newsScroller['news_date']; ?> - <?php echo str_replace("'","\\'",$row_newsScroller['news_title']); ?></a>';
<?php } while ($row_newsScroller = mysql_fetch_assoc($newsScroller)); ?>

</script>

Open in new window

0
 
LVL 4

Author Comment

by:cataleptic_state
ID: 35095545
Hi hielo,
Thats works great, can you explain what that does?

What if some items did not have a link, can I still put my if statement in without it affecting the rest?
0
 
LVL 4

Author Comment

by:cataleptic_state
ID: 35095679
sorry dont worry about that last message. I am just going mad :(
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question