Solved

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

Posted on 2011-03-10
5
315 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now