SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still can not figure out why - array loop

    Ok..i got it so it is echoing the name and the field value, however, it is only doing this for the first array...that is it. This script is driving me up the wall. Can a fresh pair of eyes look at it and tell me why the output is only for the first field name and value. Thanks.

    PHP Code:
    // output basic header info and beginning of xml tags
    $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $output .= "<Asset>";
    $output .= "<AssetType>";
    $output .= "File";

    // output basic header info and beginning of xml tags
    $result_fld mysql_query"Select * FROM file ORDER BY fileid"$dbhandle );
    $result_show mysql_query"SHOW FIELDS FROM file"$dbhandle );


    //count the number of records in the table
    $story_total_rows mysql_num_rows($result_fld);

    //build the loop to pull in values for each field
    for( $x=0$x $story_total_rows$x++) {
    while( 
    $row mysql_fetch_row($result_fld) ) {
    $i=0;
    while( 
    $row1 mysql_fetch_array($result_show) ) {
    $output .= "<Field>";
    $output .= "<Name>" .$row1[0];
    $output .= "</Name>";
    $output .= "<Values>";
    $output .= "<Value>" .$row[$i] ."</Value>";
    $output .= "</Values>";
    $output .= "</Field>";
    $i++;
    }
    }
    }

    $output .= "</AssetType>";
    $output .= "</Asset>";

    // tell the browser what kind of file is come in
    header("Content-type: text/xml");
    // print out XML that describes the schema
    echo $output;

    // close the connection
    mysql_close($dbhandle);
    ?>
    Last edited by stymiee; Jul 10, 2007 at 06:35. Reason: added php tags

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ugh!

    Code php:
    // output basic header info and beginning of xml tags
    $output = "<?xml version="1.0\" encoding=\"UTF-8\"?>";
    $output .= "<Asset>";
    $output .= "<AssetType>File</AssetType>";
     
    // output basic header info and beginning of xml tags
    $result_fld = mysql_query( "Select * FROM file ORDER BY fileid", $dbhandle );
     
    while( $row = mysql_fetch_array($result_fld) ) {
      foreach($row as $name=>$value){
        $output .= "<Field>";
        $output .= "<Name>" .htmlentities($name);
        $output .= "</Name>";
        $output .= "<Values>";
        $output .= "<Value>" .htmlentities($value)."</Value>";
        $output .= "</Values>";
        $output .= "</Field>";
      }
    }
     
    $output .= "</Asset>";
     
    // tell the browser what kind of file is come in
    header("Content-type: text/xml");
    // print out XML that describes the schema
    echo $output;
     
    // close the connection
    mysql_close($dbhandle);
    ?>
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your code is too complicated May be you should use mysql_fetch_assoc($result_fld), get rid of for loop. And just use $row[$row1[0]]. I can miss something, but I think you should think about something like that.

    oops. Good example by php_daemon

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you can tell i not quite a "noob" but have not had a lot of experience.

    Thanks daemon - that mod worked great, but one more thing...now it is iterating through the loop 2 times with output like such:

    Output
    ******************
    [ <Field>
    <Name>0</Name>

    <Values>
    <Value>30</Value>
    </Values>
    </Field>

    <Field>
    <Name>fileid</Name>

    <Values>
    <Value>30</Value>
    </Values>
    </Field>

    <Field>
    <Name>1</Name>

    <Values>
    <Value>2004-05-31 14:52:04</Value>
    </Values>
    </Field>

    <Field>
    <Name>modified</Name>

    <Values>
    <Value>2004-05-31 14:52:04</Value>
    </Values>
    </Field> ]

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh right, sorry about that, use mysql_fetch_assoc() instead of mysql_fetch_array()
    Saul

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks daemon....that did it....now I need to make it even more complex.....

    Some of these files have relations to other photos, stories, etc....need to pull those in as children

    But I will try it out, and if i have any problems, come back for assistance.

    Thanks again!

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok....so using daemon's code and it was working...have not changed anything, and now i get this message:

    XML Parsing Error: undefined entity

    It seems to happen in the "while loop" - Reason?

  8. #8
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're getting this error while running this code or when doing anything with its output (parsing the result xml document for instance)?
    Saul

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while running it. I deleted a row in the table and it fixed it...it now runs again. however, i am testing this on a table that has only a few records in it...when i go run this for "real." I need to run it on about 30000 rows, so need to solve the issue.

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh, that's FF that reports the error, doesn't it? It usually says which exact place is the problem. Try using utf8_encode() on the values.
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •