Results 1 to 5 of 5

Thread: Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Arrays

    How do i display the content of an array containing the information gathered by an SQL query without knowing the attributes/field names?

    thanks

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Arrays

    Say you want to loop through records in your database you can use something like this.

    PHP Code:

    //$query is the name of your query
    while ($Details mysql_fetch_row($query)) {
    echo 
    "Field 1 = $Details[0]<br>";
    echo 
    "Field 2 = $Details[1]<br>";
    echo 
    "Field 3 = $Details[2]<br>"//etc....


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Re: Arrays

    what happens if you don't know the number of fields within the query?

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Arrays

    print_r(arrayname);

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    144

    Re: Arrays

    cool thats a start

    comes out like this is there anyway to structure the output so that the data is layed out neatly

    2 - James Bowtell - BSc Computing - 1

    Array ( [0] => Array ( [Student_Id] => 2 [Student_FName] => James [Student_SName] => Bowtell [Tutor_Id] => 1 [Degree_Id] => 1 ) [1] => Array ( [Student_Id] => 7 [Student_FName] => Mike [Student_SName] => Spragg [Tutor_Id] => 2 [Degree_Id] => 2 ) [2] => Array ( [Student_Id] => 8 [Student_FName] => Andrew [Student_SName] => Byrne [Tutor_Id] => 3 [Degree_Id] => 3 ) [3] => Array ( [Student_Id] => 9 [Student_FName] => Craig [Student_SName] => Thomas [Tutor_Id] => 4 [Degree_Id] => 4 ) [4] => Array ( [Student_Id] => 10 [Student_FName] => Richard [Student_SName] => Forsyth [Tutor_Id] => 5 [Degree_Id] => 5 ) )

    the problem i'm having is that i will never know the content of the array as the user writes the SQL query the way they want it, so i cant write something like this

    Code:
    <html> 
      <body> 
        <h1>Results</h1> 
        <p>There are <?php echo($number) ?> student(s) studying this Module:</p>
        <ul> 
          <?php foreach($data as $row): //column alias names used here?> 
             <li><?php echo("{$row['Student_Id']} - {$row['Student_FName']} {$row['Student_SName']} - {$row['Module_Name']} - {$row['Tutor_FName']} {$row['Tutor_SName']}") ?></li> 
          <?php endforeach; ?> 
        </ul>
    	    <form name="form1" method="post" action="index.php">
          <input type="submit" name="Submit" value="Back">
        </form> 
      </body> 
    </html>
    Last edited by JamesBowtell; Mar 10th, 2006 at 08:43 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width