PDA

Click to See Complete Forum and Search --> : Arrays


JamesBowtell
Mar 9th, 2006, 03:52 PM
How do i display the content of an array containing the information gathered by an SQL query without knowing the attributes/field names?

thanks

lintz
Mar 9th, 2006, 05:27 PM
Say you want to loop through records in your database you can use something like this.



//$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....
}

JamesBowtell
Mar 9th, 2006, 05:53 PM
what happens if you don't know the number of fields within the query?

penagate
Mar 9th, 2006, 06:19 PM
print_r(arrayname);

JamesBowtell
Mar 9th, 2006, 06:36 PM
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


<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>