Im using the last.fm API to fetch XML data. I've then parsed this data and loaded my results into an array. However Im unsure of how to manipulate this data as single elements (I basically want a button for each element that will submit to a database table when the user clicks it)

(I've only included what I think is the relevant code, if any more is required just say)

$artistArray = array();
$releases = $xmlmusic->xpath('artist/similar/artist');
foreach ($releases as $artist) {
$artistResult .= '<div class="searchitem">';
$artistName = $artist->name . PHP_EOL;
$artistArray[] = $artistName;

This inputs the data into an array, but I'm really stuck on how to use it, any help is appreciated, thanks.

    4 days later

    I would use the following method.

    $artistArray = array();
    $releases = $xmlmusic->xpath('artist/similar/artist'); 
    foreach ($releases as $artist) { 
    $artistResult .= '<div class="searchitem">';
    $artistName = $artist->name . PHP_EOL;
    $artistArray[] = $artistName;
    
    $body .= "<form name='addartist' method='post' action='yourform.php'>
    $body .= "<select name='artist'>";
    foreach($artistArray as $val){
      $body .= "<option value='".$val."'>".$val."</option>";
    }
    $body .= "</select>";
    $body .= "<input type='submit' name='submit' value='add' />";
    $body .= "</form>";
    
    echo $body;
    

    Double check me for typos but I think this will take care of your problem but of course when the form is submitted you will need to handle it with something like

    if(isset($_POST["artist"])){
      $artist = $_POST["artist"];
      //mysql connect
      //mysql select db
      $query = "INSERT INTO yourtable (artist) VALUES ($artist)";
      $result = mysql_query($query);
    }
    
      Write a Reply...