Hello all, I have a small problem with javascript and php what am trying to do is re-code the following script so that the items array uses a php script and a loop to gets information from a database but i dont know how to go about it can anyone help ?

code would look abit like this but ofc it would need a loop feature to get and build all adverts from the database

items[<?php echo raw['sort']; ?>]="<a href='<?php echo raw['url']; ?>' target='_blank'><img alt='<?php echo raw['details']; ?>' src='<?php echo raw['image']; ?>' height='60' width='468' border='0'/></a>";

full script code below

<script language="JavaScript1.2">

    var howOften = 12; //number often in seconds to rotate
    var current = 0; //start the counter at 0
    var ns6 = document.getElementById&&!document.all; 

    // place your images, text, etc in the array elements here
    // this is the section i like to change to php to get items ID and image url so on ...

    var items = new Array();
        items[0]="<a href='#' target='_blank'><img alt='advertisements' src='default_banner.gif' height='60' width='468' border='0'/></a>";
        items[1]="<a href='#' target='_blank'><img alt='advertisements' src='4u_banner_three.gif' height='60' width='468' border='0'/></a>"; 

    function rotater() {
        document.getElementById("placeholder").innerHTML = items[current];
        current = (current==items.length-1) ? 0 : current + 1;
        setTimeout("rotater()",howOften*1000);
    }

    function rotater() {
        if(document.layers) {
            document.placeholderlayer.document.write(items[current]);
            document.placeholderlayer.document.close();
        }
        if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
            if(document.all)
                placeholderdiv.innerHTML=items[current];

        current = (current==items.length-1) ? 0 : current + 1; //increment or reset
        setTimeout("rotater()",howOften*1000);
    }
    window.onload=rotater;
    //-->
    </script>

hope sumone can help thanks

Yours
Simon

I am weak in javascript coding but you have given good description in javascript. I am learning about this.
thanks.

Create the javascript using PHP (see the comments in the code):

// suppose this is the rows array you get form the database
$rows = array(

    array(
        'sort' => 'sort1',
        'url' => 'url1',
        'details' => 'detail1',
        'image' => 'image1'
    ),

    array(
        'sort' => 'sort2',
        'url' => 'url2',
        'details' => 'detail2',
        'image' => 'image2'
    ),

    array(
        'sort' => 'sort3',
        'url' => 'url3',
        'details' => 'detail3',
        'image' => 'image3'
    ),
);


// prepare a string for items array in javascript code
// you will use it below in $javascript
$items_string = '';

foreach($rows as $raw) {

    // assign variables so everything is more readable
    // you can also check for values of returned rows here
    $sort = $raw['sort'];
    $url = $raw['url'];
    $details = $raw['details'];
    $image = $raw['image'];

    // compose th string
    $items_string .= "items[$sort]=\"<a href='$url' target='_blank'>";
    $items_string .= "<img alt='$details' src='$image' ";
    $items_string .= "height='60' width='468' border='0'/></a>\";";
}

// compose javascript code using heredoc syntax
// see http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
$javascript = <<<JSCODE

<script type="text/javascript">

    var howOften = 12;
    var current = 0;
    var ns6 = document.getElementById&&!document.all;

    var items = new Array();
        $items_string

    function rotater() {
        document.getElementById("placeholder").innerHTML = items[current];
        current = (current==items.length-1) ? 0 : current + 1;
        setTimeout("rotater()",howOften*1000);
    }

    function rotater() {
        if(document.layers) {
            document.placeholderlayer.document.write(items[current]);
            document.placeholderlayer.document.close();
        }
        if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
            if(document.all)
                placeholderdiv.innerHTML=items[current];

        current = (current==items.length-1) ? 0 : current + 1;
        setTimeout("rotater()",howOften*1000);
    }
    window.onload=rotater;
    //-->
    </script>
JSCODE;

// echo the javascript code on appropriate place in your html or php

echo $javascript;

If you do not like heredoc syntax you can compose the javascript code string just by concatenating each row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.