Jump to content

Array returns first letter only


rbrady

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 ?
Link to comment
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?
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.