Forum Moderators: coopster

Message Too Old, No Replies

Showing full 2d array on screen from MySQL

php newbie on first serious script

         

kiwibrit

1:27 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



I have successfully created a MySQL database, and inserted a table "65chain" into it.

The table has 10 columns, including the primary key column Drop_1, and 6 rows. The Drop_1 column is indexed at 600, 800, 1000, 1200, 1400, and 1600. The table is representative of a series which will have varying numbers of rows and columns.

In phpMyAdmin, I see the 2d array I would like to see eventually on my web page, when I select the SQL tab and enter
SELECT * FROM `65chain` WHERE Drop_1 is not null

Great. I am now creating my script.

So far I have:

<?php
$username = "#*$!#*$!#*$!";
$password = "yyyyyyyyy";
$hostname = "zzzzzzzzz";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
// select database
mysql_select_db("aaaaaaa") or die ("Unable to select database!");

// execute query
$query = "SELECT * FROM 65chain";

$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());

// iterate through rows

while ($row = mysql_fetch_row($result))
{
echo "$row";
}

// close database connection
mysql_close($dbh);

?>

I have obviously done something stupid in my ignorance - the result on the web page screen is not the 2d array I wish to see, but ArrayArrayArrayArrayArrayArray

I'd be grateful for a steer on how to display my 2d array.

dreamcatcher

1:39 pm on Jun 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

You need to use numerical indices when echoing the value using mysql_fetch_row.

echo $row[0];
echo $row[1];

etc

dc

kiwibrit

6:05 am on Jun 13, 2005 (gmt 0)

10+ Year Member



Thanks, dreamcatcher, I'll play with that.

bobnew32

7:17 am on Jun 13, 2005 (gmt 0)

10+ Year Member



To see all your data do this (just as a learning supplement)

print_r($row);

kiwibrit

9:47 am on Jun 13, 2005 (gmt 0)

10+ Year Member



Well, I am learning a lot, but still not achieving my objective.

The table consists of this particlar array - except that the ..... are used here to space the "X" and the "-" symbols corectly.

Drop_1 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600
600.......X.....X.....X....X......X......X.....X....X......X......X......X
800.......X.....X.....X....X......X......X.....X....X......X......X......-
1000......X.....X.....X....X......X......X.....X....X......X......X......-
1200......X.....X.....X....X......X......X.....X....X......-......-......-
1400......X.....X.....X....X......X......X.....X....X......-......-......-
1600......X.....X.....X....X......X......X.....X....X......-......-......-

What I am trying by stages to do is to display the "X" and the "-" in a table. The columns will be lablled to show blind (shade) width, the rows will be labelled to show vertical drop.

As a first stage, I was trying to learn how to show the full array, the next thing was to learn how to insert each "X" or "-" into a <td>, so that the spatial relationship was correct.

So, once again your forebearance with an ignoramus, please - I'd be grateful for a few more steers!

kiwibrit

1:05 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



Hmm. Getting somewhere, I have found:

while ($row = mysql_fetch_row($result))
{
echo "$row[0]",":";
echo "$row",":";
echo "$row[3]" ,":";
echo "$row[4]" ,":";
echo "$row[5]" ,":";
echo "$row[6]" ,":";
echo "$row[7]",":";
echo "$row[8]",":";
echo "$row[9]" ,":";
echo "$row[10]",":";
echo "$row[11]" ,":";
echo "$row[12]" ;
}

gives me a browser display of:

600:X:X:X:X:X:X:X:X:X:X: 800:X:X:X:X:X:X:X:X:X:-: 1000:X:X:X:X:X:X:X:X:X:-: 1200:X:X:X:X:X:X:X:-:-:-: 1400:X:X:X:X:X:X:X:-:-:-:1600:X:X:X:X:X:X:X:-:-:-:

Frustrating. The data is all there in the browser, but not stacked as I would wish - i.e.:
600:X:X:X:X:X:X:X:X:X:X:
800:X:X:X:X:X:X:X:X:X:-: --- and so on.

At the moment I have not discovered the code to do that. Any suggestions?

[1][edited by: jatar_k at 4:17 pm (utc) on June 13, 2005]
[edit reason] fixed sidescroll [/edit]

kiwibrit

1:44 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



OK, adding

echo "<br>";

at the end of the last little lot stacks things as I wanted for the first stage of my task.

Now I have to find a way of getting each value of "X" or "-" into a table cell. Not sure how to go about that, yet.

kiwibrit

4:31 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



Resolved, thanks to advice from Installer in the phpbuilder forum. This does it:

echo '<table>';
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
foreach ($row as $item) {
echo '<td>' . $item . ':</td>';
}
echo '</tr>';
}
echo '</table>';