Solved

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

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

772 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