Jump to content

PHP SQL array to js array


amwd07

Recommended Posts

Hello again

not sure if this one is possible

here's my code which now works fine in the PHP

 

<?php
  /*** create DB sql query to prepare for the array ***/  
$sql = db::getInstance()->ExecuteS('SELECT id_image, id_product FROM ps_image GROUP BY id_product');

  /*** loop over the array ***/
foreach ($sql as $row) {
   $productID = $row['id_image']; 
   $imageID = $row['id_product']; 
   $img = "img/p/".$imageID."-".$productID."-home.jpg";  
// print "product ID = $productID image ID = $imageID </br>";   
  }   
?>

 

when I try to loop the array with the js array it only echo's 1 row from the DB

not quite sure where I am goign wrong on this one  ???

 

var i=1;
for (i=1;i<=10;i++)
{
document.write(
Car_Image_Sources=new Array(
"<?php echo $img; ?>",<?php echo $_SERVER['REQUEST_URI'];?>+"product.php?id_product="+<?php echo $productID;?>
)
);
}

Link to comment
Share on other sites

Your PHP script or HTML page renders the page where your javascript code resides with the current value of the embedded variables.  In the javascript example the value of those variables does not change inside the for loop.

 

If you want to make different products accessible to javascript, you need to create an array containing product data and make the array available to javascript.  The way to do this is through AJAX queries, for example using the jQuery library.  You code can then send a request to a PHP script similar to your current PHP code which returns the data in XML format.  The response is then parsed by the jQuery code which makes the data available as a javascript array or variables.

 

Here is an example of how it might work.  The PHP script will need to extract the input parameter from the $_POST variable, retrieve the data, build the XML file, issue the header command with 'Content-type: text/xml'  and echo the XML data back to the browser.

 

function get_products() {
  var url = 'get_products.php';
  data = 'lines=' + '10'; // request data
$.ajax({
  type: "GET",
  url: url,
  data: data,
  success: function(xml) {
    var output = '<p><ul>';
    var productid = '';
    var imageid = '';
    $("line", xml).each(function(i) {
        productid = $(this).find('ProductID').text();
        image = $(this).find('ImageID').text();
  	output += '<li>img/p/' + image + '-' + productid + '-home.jpg' + '</li>';
    });
    output += '</ul></p><hr />';
    $('#product_list').html(output); // write html to document
  }
}); //ajax
}

 

 

Link to comment
Share on other sites

No I am even more confused

I come up with something else which I think may be closer to my solution

products won't be called via GET just what ever is in the sql query  :-X

 

<?php
  /*** create DB sql query to prepare for the array ***/  
$sql = db::getInstance()->ExecuteS('SELECT id_image, id_product FROM ps_image GROUP BY id_product');

header("Content-Type: text/javascript"); 
// Start looping rows in mysql database. 
foreach ($sql as $row) {
   $productID = $row['id_image']; 
   $imageID = $row['id_product']; 
   $img = "img/p/".$imageID."-".$productID."-home.jpg";  
// print "product ID = $productID image ID = $imageID </br>"; 
?> 
Car_Image_Sources=new Array["<?php echo $img; ?>",<?php echo $_SERVER['REQUEST_URI'];?>+"product.php?id_product="+<?php echo $productID;?>] 
<? 
// close while loop 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.