Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-10
5
Medium Priority
?
327 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

730 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