Jump to content


Photo

Array returns first letter only


  • Please log in to reply
3 replies to this topic

#1 rbrady

rbrady
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 October 2006 - 04:49 PM

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
<?
	$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++;
	}
?>
then I do like
<? echo($team1name[8]); ?>
or
<? echo($team1score[0]."-".$team2score[0]); ?>
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
mysql_result($result,$i,"team1name");
i get the full team name, but when i try to call
<? echo($team1name[8]); ?>
i just get the first letter. any help ?


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 11 October 2006 - 05:02 PM

try this as an alternative way to populate your arrays and see if it helps at all:
<?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');
}
?>

what does that get you?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 rbrady

rbrady
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 October 2006 - 05:05 PM

great! it worked, thanks so much  ;)

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 October 2006 - 05:14 PM

If you're open to other suggestions on alternative methods, here's how I would do this:
<?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.
?>

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users