/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Problems with Arrays

I am not that good with arrays but here is what I have.

[code=php]<?php
include(‘includes/db.php’);

$colors = array(“#FF0000″,”#0000FF”,”#00FF00″,”#BEBEBE”,”#C6E2FF”,”#8A2BE2″,”#FF8C00″,”#FF69B4″,”#FAEBD7″);

$query = “SELECT brand FROM computer GROUP BY brand ORDER BY brand”;
$result = mysql_query($query);
echo “<table>”;

while($row = mysql_fetch_array($result) && list($key,$color) = each($colors)){
echo “<tr bgcolor=’$color’><td>”, $row[‘brand’], “</td></tr>”;
}

echo “</table>”;
echo “<h1>This is a test</h1>”;
echo “<img src=’chart.php’>”;
?>[/code]

Why won’t this work?

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 21.2009 — Erm, what is it supposed to do?
Copy linkTweet thisAlerts:
@monarch_684authorJan 21.2009 — I have a pie chart that is created using php. I am wanting to get the legend to print in a table with the color of the brand in the pie chart as the background in the table row.
Copy linkTweet thisAlerts:
@scragarJan 21.2009 — Try this:
[code=php]

include('includes/db.php');

$colors = array("#FF0000","#0000FF","#00FF00","#BEBEBE","#C6E2FF","#8A2BE2","#FF8C00","#FF69B4","#FAEBD7");
reset($colors);
$query = "SELECT brand FROM computer GROUP BY brand ORDER BY brand";
$result = mysql_query($query);
echo "<table><tbody>";

while($row = mysql_fetch_array($result) && list($key,$color) = each($colors)){
echo "<tr bgcolor='{$color}'><td>{$row['brand']}</td></tr>";
}

echo "</tbody></table>";
echo "<h1>This is a test</h1>";
echo "<img src='chart.php'>";
[/code]

TBH though I really want to know what the result of the code is, I can't see why it wouldn't work.
Copy linkTweet thisAlerts:
@monarch_684authorJan 21.2009 — Nope didn't work. Here is my test page so you can see what it is doing.

http://www.gcncs.net/stats.php

It does the colors fine but is won't list the brand name of the computers.
Copy linkTweet thisAlerts:
@mintedjoJan 21.2009 — The original post was using commas to concatenate instead of dots.

That might be why it wasnt working originally.

What happens if you just try to echo $row['brand'] ?
Copy linkTweet thisAlerts:
@scragarJan 21.2009 — Huh, why is that?
[code=php] while($row = mysql_fetch_array($result) && list($key,$color) = each($colors)){
echo "<tr bgcolor='{$color}'><td>";
var_dump($row);
echo "{$row['brand']}</td></tr>";
}
[/code]
Copy linkTweet thisAlerts:
@mintedjoJan 21.2009 — [CODE]while(($row = mysql_fetch_array($result)) && list($key,$color) = each($colors)){
echo "<tr bgcolor='{$color}'><td>{$row['brand']}</td></tr>";
}[/CODE]


Perhaps ? It looks like $row is being assigned a boolean which can only be happening due to operator precedence.
Copy linkTweet thisAlerts:
@monarch_684authorJan 21.2009 — [code=php]var_dump($row);[/code]
gave me: bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) [/QUOTE]

What happens if you just try to echo $row['brand'] ? [/QUOTE]

Nothing. When I put the array in there the mysql stopped working.
Copy linkTweet thisAlerts:
@mintedjoJan 21.2009 — Have u tried using the while loop i posted above?

Unless your database is full of boolean values all containing true then thats probably the solution.
Copy linkTweet thisAlerts:
@monarch_684authorJan 21.2009 — THANKS MINTEDJO!!!!!!!

It works now.
×

Success!

Help @monarch_684 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...