Ryan Williams
Ryan Williams

Reputation: 663

Extract parts of a php array to client side

So I'm trying to centralize products in one central php file and have my client side php just request info so I only have to edit the central php file to add and remove products

my server side

 $varProduct= (

//      [0]      [1]   [2] [3 4 5 6 7]             [8]  
array("Title" , 0001 , 100, 0,0,1,1,0, "/womens/tops/s/2.png", "/womens/tops/s/2.jpg", "/womens/tops/s/2.jpg", 50  )

 )

In my html client side I want to display the title, the price [2] and the url [8] basically

for(i=o, i< $varProduct.length(), i++){

//display $varProduct[i][0];
//display the Image for $varProduct[i][8];
//display  $varProduct[i][2];

}

how can I put values in my server side file to my client side in within html tags? I need to display them inline will I be able to format the variables?

Upvotes: 0

Views: 98

Answers (1)

Faiz Rasool
Faiz Rasool

Reputation: 1379

Try something like this

<?php
for ($i = 0; $i < count($varProduct); $i++) {
    //full path -- then post pram
    $return =  sendPostData("http://stackoverflow.com/", array('parm1' => $varProduct[$i][0], 'parm2' => $varProduct[$i][0]));
    print_r($return);
}
?>

<?php

//send data function
function sendPostData($url, Array $post) {
    $data = "";
    foreach ($post as $key => $row) {
        $row = urlencode($row); //fix the url encoding
        $key = urlencode($key); //fix the url encoding                
        if ($data == "") {
            $data .="$key=$row";
        } else {
            $data .="&$key=$row";
        }
    }
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $result = curl_exec($ch);
    curl_close($ch);  // Seems like good practice
    return $result;
}
?>

Upvotes: 1

Related Questions