Forum Moderators: coopster

Message Too Old, No Replies

Inserting database values into an array.

         

dkin

7:21 pm on Jun 20, 2004 (gmt 0)

10+ Year Member



I have my item database almost complete, but there are a few things I must tweak.

This is what I need.

I have many attributes for each item

AC
AR
PR
FR
Hp
Pow
etc
etc

I am wanting to display only the attributes which have values and I beleive an array would be the easiest way to do this.

So my first question.

Can I put mysql database values into an array?

I tried and it did not work. I used this as my value

$row["AC"];

If someone could correctly insert this into an array and show me I would be very thankful.

Second is how would I go about only showing the attribute name if there is a value in it?

coopster

6:16 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK. Now look closer at the loop construct to see how it is working. We will move the table columns outside of the loop that builds each column of your database table and add a couple of html non-breaking spaces (we'll also put your key back in)...
while ($row = mysql_fetch_array($result)) { 
print '<tr>';
print '<td>';
foreach ( $row as $key => $val ) {
if (!empty($val) && $key!= "Item_Name" ) {
print $key . " : " . $val . '&nbsp;&nbsp;';
}
}
print '</td>';
print '</tr>';
}

[edited by: coopster at 6:20 pm (utc) on June 23, 2004]

dkin

6:20 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



now how would I get the name of the attribute infront of the value?

coopster

6:21 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I updated the previous message while you were posting. You just have to add the key back in...

dkin

7:24 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



For some reason my form will not successfully submit to my db when I have fields named Range and Int.

Anyone know why?

coopster

7:29 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



They are reserved words.

[dev.mysql.com...]

dkin

7:42 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



I figured as much.

Thx it works again.

dkin

8:18 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Now when I enter that code into my main page it only shows one item from my db.

But it takes the values specified from all items and inputs it into each td.

I only want the attributes which match the item name or id to be displayed in the same <tr>

dkin

9:54 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



anyone know?

coopster

5:04 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You'll have to incorporate some more logic, particularly some control structures [php.net] to compare [php.net] data and determine whether or not you want to display them.
This 39 message thread spans 2 pages: 39