0

After resolving an Undefined offset error, the error message is no more, but the results now displayed are only one record instead of the expected number of records, for instance 3.

$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
    $time = $row['vluchttijd'];
    if (!function_exists('timeInWords')) {
    function timeInWords($time) {
        list($hours, $minutes) = explode(':', $time);
        return (int)$hours .  " hrs " . (int)$minutes . " min";
   }    $result = mysql_query($sql);

{

I can remove everything from $time down and then it returns all of the expected records.

As requested all of the code. I should preface this that I am still very very new to the world of PHP, so please go easy on me:

<?php

include('../datalogin.php');    // include your code to connect to DB.


mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
 die("Invalid ID specified.");
} 
$rID = (int)$_GET['id'];

$sql = "a bunch of SQL code removed to save space

 WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";

$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$time = $row['vluchttijd'];
if (!function_exists('timeInWords')) {
function timeInWords($time) {
    list($hours, $minutes) = explode(':', $time);
    return (int)$hours .  " hrs " . (int)$minutes . " min";
}   $result = mysql_query($sql);

{
echo "<table border='0' width='640'>";
echo "<tbody>";
echo "<tr>";
echo "<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2'])). "  
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_vertrek'>(D)  
".htmlspecialchars($row['luchthavennaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='18%'><strong>Duration:</strong></td>";
echo "<td width='41%'>".timeInWords($time)."</td>";
echo "<td rowspan='4' width='41%' align='center' valign='middle'><img    
src='../logos/".$row['logo']."'</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Equipment:</strong></td>";
echo "<td>".$row['toestel']."&nbsp;".$row['erlr']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Class:</strong></td>";
echo "<td>".$row['reisklass']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Miles:</strong></td>";
echo "<td>".$row['afstand']." miles</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='12'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_aankomst'>(A) 
".htmlspecialchars($row['aankomstnaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='1' bgcolor='#585858'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
  • 1
    which variable is undefined? – Mohammad Kermani May 10 '14 at 02:21
  • see this http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Mohammad Kermani May 10 '14 at 02:23
  • Oops, post all of your code for me. – user3587554 May 10 '14 at 02:32
  • as requested I have added the code user3587554 –  May 10 '14 at 02:41
  • why are you type casting $_GET[$rID] but checking if it's numeric? It's overkill. if is_numeric is true then it's already a number. The type cast on hrs and min also is not needed. echo does not care if it's int or not unless it's an object or array. You'd only type cast it if you had it in another format. you could still perform math on it as it will be converted to a number. What is the extra mysql_result() for after the while loop? – user3587554 May 10 '14 at 02:49
  • Also you need your display info in the while loop. You can create a placeholder array and store each row in it to be used on the outside or you can use the while loop to output the data as you need it to display. – user3587554 May 10 '14 at 02:52
  • @user3587554 I would not say its the best way, but in this case, is_numeric would return true on a float or other numeric formats other than int. I always recommend intval() and fail on <=0 – Cleric May 10 '14 at 02:53
  • true, I've never type casted on input and I would of sanitized the input for integrity sake. – user3587554 May 10 '14 at 02:55
  • thank you both for your responses. this worked how I would expect it. Regarding the above other comments. I am a self taught PHP user. I am still learning. I only work on my own personal website and have a lot of learning to do. thank you for your help. –  May 10 '14 at 03:00

1 Answers1

0

This should do it, you called the mysql_query with the same query over again, so that would give you the same result all the time.

I cleaned the start of you while loop, so it only fetches the data from your result object and prints it. Your time function does not need to be inside the loop so I moved it outside.

I also cleaned the loop from some echo's

<?php

include('../datalogin.php');    // include your code to connect to DB.

function timeInWords($time) {
    list($hours, $minutes) = explode(':', $time);
    return (int)$hours .  " hrs " . (int)$minutes . " min";
}


mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
 die("Invalid ID specified.");
} 
$rID = (int)$_GET['id'];

$sql = "a bunch of SQL code removed to save space

 WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
$time = $row['vluchttijd'];    

echo "<table border='0' width='640'>
<tbody>
<tr>
<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_vertrek'>(D)  
".htmlspecialchars($row['luchthavennaam'])."</span></td>
</tr>
<tr>
<td width='18%'><strong>Duration:</strong></td>
<td width='41%'>".timeInWords($time)."</td>
<td rowspan='4' width='41%' align='center' valign='middle'><img    
src='../logos/".$row['logo']."'</td>
</tr>
<tr>
<td><strong>Equipment:</strong></td>
<td>".$row['toestel']."&nbsp;".$row['erlr']."</td>
</tr>
<tr>
<td><strong>Class:</strong></td>
<td>".$row['reisklass']."</td>
</tr>
<tr>
<td><strong>Miles:</strong></td>
<td>".$row['afstand']." miles</td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3' height='12'></td>
</tr>
<tr>
<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_aankomst'>(A) 
".htmlspecialchars($row['aankomstnaam'])."</span></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3' height='1' bgcolor='#585858'></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>";


}
echo "</table>";
?>
Cleric
  • 3,167
  • 3
  • 23
  • 24
  • mysql_fetch_assoc() is better than mysql_fetch_array() with saying the type or you get an array double in size with duplicates. http://www.w3schools.com/php/func_mysql_fetch_array.asp you only need the parts printed by the actual loop or your going to have a new table on each pass of the loop. – user3587554 May 10 '14 at 03:02
  • 1
    no prob we gotta teach the new generation the best way so php doesn't keep getting bad rep – user3587554 May 10 '14 at 03:04