i have an array that is in a while loop to parse the information in a database. Problem is, there is 4 arrays, one of them only displays the first character from the field. i wrote a test page using the same loop and it gets the whole string. the array in question is $time[$i]

here is the code i am using:
mysql_connect("localhost", "username", "xxxxxx");
mysql_select_db("database");

$query = "SELECT * FROM trouble";
$result = mysql_db_query("database", $query);

$numrows = mysql_numrows($result);

echo ("<center>");

while ( $i < $numrows) {
$time[$i] = mysql_result($result, $i, time);
$trouble[$i] = mysql_result($result, $i, trouble);
$tech[$i] = mysql_result($result, $i, tech);
$num[$i] = mysql_result($result, $i, num);

    $i++;

}
krsort($num);
foreach($num as $var1 => $var2){
echo ("
<br>
<form method=\"POST\" action=\"add.php\">
<table width=\"400\" border=0 bgcolor=999999>
<tr>
<td>
$time[$var1]
</td>
<td align=right>
<b>$tech[$var1]</b>
</td>
<td align=right width=\"10%\">
<input type=\"submit\" name=\"add\" value=\"add\"></input>
</td>
</tr>
<tr><td colspan=3><hr></td></tr>
<tr>
<td colspan=3>
$trouble[$var1]
</td>
</tr>
</table>
<input type=\"hidden\" name=\"index\" value=\"$num[$var1]\"></input>
</form>
");
}

in the above code snippet $time[$var1] only displays the first character of the string (ie "T");

this code works though and displays all of the string(ie "Thursday January 30 2003 02:35 ":

<?php
mysql_connect("localhost", "username", "xxxxxx");
mysql_select_db("krisarnold");

$query = "SELECT * FROM trouble";
$result = mysql_db_query("krisarnold", $query);

$numrows = mysql_numrows($result);

echo ("<center>");

while ( $i < $numrows) {
$time[$i] = mysql_result($result, $i, time);
$trouble[$i] = mysql_result($result, $i, trouble);
$tech[$i] = mysql_result($result, $i, tech);
$num[$i] = mysql_result($result, $i, num);

    $i++;

}
krsort($num);
foreach($num as $var1 => $var2){
echo "$time[$var1]<br>";
}
mysql_close();
?>

i cant distinguish at all what is making one work and the other not. hopin maybe someone here can lend a helping hand

    I think you may want the field to be a string, as in

    time[$i] = mysql_result($result, $i, "time");

    I actually have never used mysql_result

    PHPdev

      nope nothing, i think to maybe open up the picture completly i will post the entire script, it has 2 while loops to gather information about a trouble report. the first while loop gets only the first character of the string, the second gets the whole string. wierd thing is that i copy/pasted the code from the first while loop and just changed the variable names for the second loop:

      <html>
      <head>
      <title>Trouble Reporting</title>
      </head>
      <body>
      <?php
      $time = date("l F d Y h:i" ,time());
      ?>
      <a href="search.php">Search Trouble</a>
      <form method="POST" action="<?= $PHP_SELF ?>">
      <input type="hidden" name="date" value="<?= $time ?>"></input>
      <center>
      <table border=0 cellspacing=0 bgcolor=#999999>
      <tr>
      <td align=center colspan=2>
      <font face="Arial"><b>Operations Trouble Report</b></font>
      </td>
      </tr>
      <tr>
      <td>
      <font face="Arial"><?= $time ?></font>
      </td>
      <td width=30% align=right>
      <font face="Arial">Tech:</font>
      <SELECT name="tech">
      <option value="prf">prf</option>
      <option value="ctg">ctg</option>
      <option value="dela">dela</option>
      <option value="tomn">tomn</option>
      <option value="kris">kris</option>
      <option value="conor">conor</option>
      <option value="jasonh">jasonh</option>
      <option value="sdugan">sdugan</option>
      </select>
      </td>
      </tr>
      <tr>
      <td colspan=2>
      <font face=\"Arial\">Company Name:</font>
      </td>
      </tr>
      <tr>
      <td colspan=2>
      <input type="text" name="company" size=60 maxlength=100></input>
      </td>
      </tr>
      <tr>
      <td colspan=2>
      Trouble:
      </td>
      </tr>
      <tr>
      <td colspan=2>
      <textarea name="trouble" cols=45 rows=10></textarea>
      </td>
      </tr>
      <tr>
      <td align=center colspan=2>
      <input type="submit" name="submit" value="submit"></input>
      </td>
      </tr>
      </table>
      </center>
      </form>
      <?php
      if ($submit == "submit") {
      mysql_connect("host", "user", "xxxxx");
      mysql_select_db("database");

          $query = "INSERT INTO trouble (time, trouble, tech, company) VALUES ('$time', '$trouble', '$tech', '$company')";
          $result = mysql_db_query("database", $query);
      
          if (!$result){
            echo (" Error submiting Trouble ");
          } else {
            echo ("
                  <script language=\"Javascript\">
                          window.location=\"$PHP_SELF"."?submit=1\";
                  </script>
            ");
          }
          mysql_close();

      } elseif ($submit == "1") {
      echo ("
      <center><font face=\"Arial\" color=#FF3333><b>Trouble Reported</b></font></center>
      ");
      }

      mysql_connect("host", "user", "pass");
      mysql_select_db("database");

      $query = "SELECT * FROM trouble";
      $result = mysql_db_query("database", $query);

      $numrows = mysql_numrows($result);

      echo ("<center>");

      while ( $i < $numrows) {
      $time[$i] = mysql_result($result, $i, time);
      $trouble[$i] = mysql_result($result, $i, trouble);
      $tech[$i] = mysql_result($result, $i, tech);
      $company[$i] = mysql_result($result, $i, company);
      $num[$i] = mysql_result($result, $i, num);

          $i++;

      }
      krsort($num);
      foreach($num as $var1 => $var2){
      echo ("
      <br>
      <form method=\"POST\" action=\"add.php\">
      <table width=\"400\" border=0 bgcolor=999999>
      <tr>
      <td>
      $time[$var1]
      </td>
      <td align=right>
      <b>$tech[$var1]</b>
      </td>
      <td align=right width=\"10%\">
      <input type=\"submit\" name=\"add\" value=\"add\"></input>
      </td>
      </tr>
      <tr>
      <td colspan=3>
      <b>$company[$var1]</b>
      </td>
      </tr>
      <tr>
      <td colspan=3>
      $trouble[$var1]
      </td>
      </tr>
      </table>
      <input type=\"hidden\" name=\"index\" value=\"$num[$var1]\"></input>
      </form>
      ");

          $extendquery = "SELECT * FROM extendtrouble WHERE refer = '$num[$var1]'";
          $extendresult = mysql_db_query("database", $extendquery);
          $extendnumrows = mysql_numrows($extendresult);
      
          while ($x < $extendnumrows){
                  $extendtime[$x] = mysql_result($extendresult, $x, time);
                  $extendtrouble[$x] = mysql_result($extendresult, $x, trouble);
                  $extendtech[$x] = mysql_result($extendresult, $x, text);
                  $extendnum[$x] = mysql_result($extendresult, $x, num);
                  $extendcompany[$x] = mysql_result($extendresult, $x, company);
      
         echo (" 
          <table width=\"400\" border=0 bgcolor=#CCCCCC>
          <tr>
            <td>
                  $extendtime[$x]
            </td>
            <td align=right>
                  <b>$extendtech[$x]</b>
            </td>
          </tr>
          <tr>
            <td colspan=2>
                  $extendcompany[$x]
            </td>
          </tr>
          <tr>
            <td colspan=2>
                  $extendtrouble[$x]
            </td>
          </tr>
          </tr>
          <tr><td colspan=2><hr></td></tr>
          </table>
          ");
          $x++;
          }
          $x=0;

      }

      mysql_close();
      ?>
      </center>
      </body>
      </html>

        What if just after the first while loop you do a die(mysql_result($result, $i, time))

        This should print out what is returned by the mysql_result()... this will then tell you if the issue lies in your data or the assignment...

        There always is more than one way to skin a cat, but you can execute your loop like this too...

        while ($data = mysql_fetch_array($result)) {
        echo("<!-- HTML CODE HERE -->")
        }

        Then you can just do $data["time"] for when you want to insert the time...

        Just a thought.. .

        PHPdev

          hey, PHP dev

          you said you have never used mysql_result.....what do you use?

            99% of the time I use mysql_fetch_object()... and do my while loops as

            while($data = mysql_fetch_object()) {}

            PHPdev

              i added die(mysql_result($result, $i, time)); to the loop and it comes back with the full string instead of just the first letter. hope that helps, in the mean time i am going to play with your other suggestion.

                Ok something else you can look at is, you are using $var1 in the foreach loop after sorting the array $num, it does make sense that this should be the index into the arrays, but I would also make sure that these are correct...

                You can do a print_r($num) this will print all the elements of the array, this will tell you what you are looking at in $var1

                Just another thought

                PHPdev

                  all of the elements in the array look to be correct, that is it is getting the right information according to the index number. I was playing around with the mysql_fetch_array() suggestion you made, and i am not sure how i would put it all together with the krsort() and foreach() loop. i do have to say thanks for the valuable information you have given so far. sooner or later i will nail this one down

                    Sorry my mysql_fetch_array() may need a little more explaining...

                    There are a couple of ways that come to mind on how you can do the krsort (If I am correct the kr sort sorts high to low using the array index)

                    1. Is use the ORDER BY clause in your query, I think you want the most recent log at the top?

                    2. You can still use the krsort and foreach loop but you while loop would look like this...

                    while ($data = mysql_fetch_array($result)) {
                    $time[$i] = $data->time;

                    $i++;
                    }

                    But since you can do a print_r($num) or print_r($time), what ever arrays you are having issues with, we know the data is there, it is just not gettting displayed correctly.

                    Everything looks ok in your echo() The only thing I can suggest here is you can try echo "<form ..." . $time[$var1] . "... </form>"; useing the "." concatination operator and taking your arrays out of the string. I dont see how this will help... but it is kind of the last thing that I can think of...

                    PHPdev

                      well i tried setting up the while loop using the mysql_fetch_array() function that you mentioned. still didnt work. I think i am gonna have to call it a day on this one. I do like the way that you setup your while loops with that function though and i think i am gonna start doing it that way. Thanks 😃

                        SOAB i just copied the loop section into a test file and it worked just fine. so i figured that the problem must not be in the loop, its somewhere else. this whole time i over looked one snippet of code at the very top of the script:
                        <?php
                        $time = date("l F d Y h:i" ,time());
                        ?>

                        I had already defined $time as a variable. renamed the $time variable to $Etime and bang it worked. thanks for your help PHPdev i can sleep easier tonight

                          Write a Reply...