Jump to content

Archived

This topic is now archived and is closed to further replies.

rbrady

Array returns first letter only

Recommended Posts

Hello, I am trying to make tournament brackets, so I need to put all the info into an array so i can call each team name/score when necessary. I currently am using
[code]<?
$query="SELECT * FROM results";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$team1name[$i]=mysql_result($result,$i,"team1name");
$team2name[$i]=mysql_result($result,$i,"team2name");
$team1score[$i]=mysql_result($result,$i,"team1score");
$team2score[$i]=mysql_result($result,$i,"team2score");
$i++;
}
?>[/code]
then I do like
[code]<? echo($team1name[8]); ?>[/code]or[code]<? echo($team1score[0]."-".$team2score[0]); ?>[/code]
but I only get the first letter returned. I am sure its pulling the correct info, and I am sure the full team name is in the db. if i echo [code]mysql_result($result,$i,"team1name");[/code] i get the full team name, but when i try to call [code]<? echo($team1name[8]); ?>[/code] i just get the first letter. any help ?

Share this post


Link to post
Share on other sites
try this as an alternative way to populate your arrays and see if it helps at all:
[code]
<?php
$team1name = array();
$team2name = array();
$team1score = array();
$team2score = array();
$sql = mysql_query("SELECT * FROM results");
for ($i = 0; $i < mysql_num_rows($sql); $i++) {
  $team1name[] = mysql_result($sql, $i, 'team1name');
  $team2name[] = mysql_result($sql, $i, 'team2name');
  $team1score[] = mysql_result($sql, $i, 'team1score');
  $team2score[] = mysql_result($sql, $i, 'team2score');
}
?>
[/code]

what does that get you?

Share this post


Link to post
Share on other sites
If you're open to other suggestions on alternative methods, here's how I would do this:
[code]<?php
$teams = array();
$q = "select * from results";
$rs = mysql_query($q) or die("Problem with query: $q<br>" . mysql_error());
while($rw = mysql_fetch_assoc($rs)) {
  $teams[$rw['team1name']] = $rw['team1score'];
  $teams[$rw['team2name']] = $rw['team2score'];
}
echo '<pre>' . print_r($teams,true) . '</pre>'; // just to show what the array $teams has in it.
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.