Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Accordion won't close correctly 5 13
PHP SMTP authentication 6 27
Getting selected value using Jquery 3 20
Table header must be on top 2 27
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

749 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