Jump to content

[SOLVED] Foreach statement wont stop


AE117

Recommended Posts

Heres the code to start with

 

<?php

$host = "";
$user = "";
$pass = "";
$database = ";


if(!$dbconnect = mysql_connect($host, $user, $pass)) {
   echo "Connection failed to the host 'localhost'.";
   exit;
} // if
if (!mysql_select_db($database)) {
   echo "Cannot connect to database";
   exit;
} // if

header("Content-Type: text/xml");

$table_id = 'ost_media';
$query = "SELECT * FROM $table_id";
$dbresult = mysql_query($query, $dbconnect);

$outer_table = 'ost_media';
$query = "SELECT * FROM ost_media WHERE media_title='1997 Eagle Talon";
$resouter = mysql_query($query, $dbconnect);

$inner_table = 'ost_media';
$query = "SELECT * FROM $inner_table WHERE media_access='private'";
$resinner = mysql_query($query, $dbconnect);

$dom = new DomDocument('1.0','utf-8');

$root = $dom->createElement('featureset');
$root = $dom->appendChild($root);

$row = @mysql_fetch_assoc($resouter);

// process all rows of the inner/many/child table
while ($row = @mysql_fetch_assoc($resinner)) {
    // add node for each record
$outer = $dom->createElement('album');
$outer = $root->appendChild($outer);
    $outer->setAttribute('name', 'Shock Value');
$outer->setAttribute('author', 'Timbaland');
$outer->setAttribute('imageUrl', 'images/Timbaland/image.jpg');
$outer->setAttribute('link', 'http://flabell.com/');
    // add a child node for each field
    foreach ($row as $fieldname => $fieldvalue) {
$inner = $dom->createElement('song');
    $inner = $outer->appendChild($inner);
$inner->setAttribute('name', 'name');
$inner->setAttribute('duration', '3:15');
$inner->setAttribute('buy', 'false');
$inner->setAttribute('download', 'true');
$inner->setAttribute('downloadsource', 'download/song1.mp3');
$value = $dom->createTextNode('songs/song1.mp3');
$value = $inner->appendChild($value);

    } // foreach

} // while


// get completed xml document
$xml_string = $dom->saveXML();
echo $xml_string;

?>

 

and here is what is outputs

 

<featureset>
−
<album name="Shock Value" author="Timbaland" imageUrl="images/Timbaland/image.jpg" link="http://flabell.com/">
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
<song name="name" duration="3:15" buy="false" download="true" downloadsource="download/song1.mp3">songs/song1.mp3</song>
</album>
</featureset>

 

What the probelm is that is is creating a new song row for every column there is in the table, What i need it do do is only create a song row if it matches a certain element, can anyone help me out?

Link to comment
Share on other sites

What are you trying to match it to?

 

well the foreach is looking at $row which is a mysql that is looking for a title in a table called "1997 Eagle Talon"

 

And i want that foreach to stop when i matchs that title, However i do think my foreach statement is off.

 

I have tried foreach ($row = $value)

 

$value  = '1997 Eagle talon';

 

but i get errors left and right including a DOM exception

Link to comment
Share on other sites

A brief look over this suggests that you should probably be doing the record selection in the SQL (which you nearly are I think).

 

You've got two queries, querying the same table. What is the inner query for? Looks as though if you just used the outer query everything would work fine. 

Link to comment
Share on other sites

A brief look over this suggests that you should probably be doing the record selection in the SQL (which you nearly are I think).

 

You've got two queries, querying the same table. What is the inner query for? Looks as though if you just used the outer query everything would work fine.

 

The inner table is actually for the album selction so if i want more then one album level I change it using that mysql call.

 

Then the outer is to select the songs

 

orignally i wrote it wron so i know they are switched around

Link to comment
Share on other sites

this has a syntax error in it .. do you have error_reporting on?

 

$query = "SELECT * FROM ost_media WHERE media_title='1997 Eagle Talon";

 

missing an ending ' after 1997 Eagle Talon

 

Totally missed that one and i guess i dont becuase i didnt pull any error in dw or log.

 

But I did change that and it didnt make a difference it is still pulling every column

Link to comment
Share on other sites

Looks as though if you just used the outer query everything would work fine.

 

Not sure what you mean by this the while statement uses the resinner query to create the levels at albumn

 

So right now i have the query set to private which i have entry however if i set it to public where i have over 100 entry then 100 levels of album are created.

 

My problem is that i need the songs inside the album to only show if the are equal to a certain element, Currently the songs that are showing is becuase the foreach statement is creating a song for each column that is in the table ost_media.

 

What I am trying to get it to do is call the query outer where the query is set to media_title "1997 Eagle Talon". There are two of these values with in the column within the table hence 2 songs should show not 22.

 

And I did change the it to outer and that doesnt work.

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.