Need Help Debugging

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Need Help Debugging

djXternal
Ok guys I have a small loop for cycling through mysql entries and it is having problems somewhere that it will not get the last entry in the db.... What should happen during this section of the script is it should cathc 12 entries with P as the "Type" and then one entry at the end with a "Type" of M... But for some reason it only catches the 12 P's and not the one M

I cannot not find the problem so I'm hoping for someone with a little more experience can find the problem.

Thanks

CODE

$counter = 0;

while($row = mysql_fetch_array($result))
   {
      if ($counter != 4)
         {
    if ($row["Type"] == P)
                   {
                      echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";
                   }
    else
                   {
          echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";                       }
    $counter++;
         }
      else
         {
    echo "</tr><tr>";
                $counter = 0;
         }
}

 

 

 


Reply

DrK3055A
Let's translate it to pseudocode:

CODE

counter=0

While (result = query to database) is true then
    if counter not equal to 4 then
        if P display location of P, else display location of M
        counter=counter+1;
    else
        display separator for html table.
        counter=0
    end if
end while

what i see from this pseudocode is that the last of each five queries won't display, else the code for the 5th lap of the while loop will set the counter to 0.

For displaying the whole content of the database i'd do:

CODE

While (result = query to database) is true then
        if P display location of P, else display location of M
        if  remainder(counter/4) is 3 display separators.
                                counter=counter+1;
end while


I'll program in another manner:
CODE

For counter=0 while (result=query to database) is true step 1 each time
        if P display location of P, else display location of M
        if  remainder(counter/4) is 3 display separators.
end for


And this last one, translated to your script:
CODE

for($counter=0;$row = mysql_fetch_array($result));$counter++) {

    if ($row["Type"] == P)    echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";
    else   echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";
    
    if($counter%4==3)  echo "</tr><tr>";
         }

 

 

 


Reply

djXternal
QUOTE(DrK3055A @ Oct 31 2006, 10:11 PM) *

Let's translate it to pseudocode:

CODE

counter=0

While (result = query to database) is true then
    if counter not equal to 4 then
        if P display location of P, else display location of M
        counter=counter+1;
    else
        display separator for html table.
        counter=0
    end if
end while

what i see from this pseudocode is that the last of each five queries won't display, else the code for the 5th lap of the while loop will set the counter to 0.

For displaying the whole content of the database i'd do:

CODE

While (result = query to database) is true then
        if P display location of P, else display location of M
        if  remainder(counter/4) is 3 display separators.
                                counter=counter+1;
end while


I'll program in another manner:
CODE

For counter=0 while (result=query to database) is true step 1 each time
        if P display location of P, else display location of M
        if  remainder(counter/4) is 3 display separators.
end for


And this last one, translated to your script:
CODE

for($counter=0;$row = mysql_fetch_array($result));$counter++) {

    if ($row["Type"] == P)    echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";
    else   echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";
    
    if($counter%4==3)  echo "</tr><tr>";
         }




Thank you so much for the help, I just couldnt see the problem, possibly because I've been coding all day

But thanks again

Reply

TavoxPeru
QUOTE(djXternal @ Oct 31 2006, 07:02 PM) *

Ok guys I have a small loop for cycling through mysql entries and it is having problems somewhere that it will not get the last entry in the db.... What should happen during this section of the script is it should cathc 12 entries with P as the "Type" and then one entry at the end with a "Type" of M... But for some reason it only catches the 12 P's and not the one M

I cannot not find the problem so I'm hoping for someone with a little more experience can find the problem.

Thanks

CODE

$counter = 0;

while($row = mysql_fetch_array($result))
   {
      if ($counter != 4)
         {
    if ($row["Type"] == P)
                   {
                      echo "<td><a href='" . $row["Location"] . "'><img src='http://fjor.homeip.net" . $row['Location'] . "' width='175px' border='0'></a></td>";
                   }
    else
                   {
          echo "<td><a href='dispMOV.php?file=" . $row["Location"] . "'><img src='media/movie.jpg' width='175px' border='0'></a></td>";                       }
    $counter++;
         }
      else
         {
    echo "</tr><tr>";
                $counter = 0;
         }
}


I dont completely understand your task, but, why you dont do 2 selects for this, the first one select the last 12 records of type P and then select the last record of type M, something like this:
CODE
// obtain the last 12 records of type P
$result=mysql_query("select * from table where table.type='P' order by table.id DESC limit 12") or die(mysql_error());
// rest of your code

// obtain the last record of type M
$result=mysql_query("select * from table where table.type='M' order by table.id DESC limit 1") or die(mysql_error());
// rest of your code

Best regards,

Reply

djXternal
QUOTE(TavoxPeru @ Nov 3 2006, 07:33 PM) *

I dont completely understand your task, but, why you dont do 2 selects for this, the first one select the last 12 records of type P and then select the last record of type M, something like this:
CODE
// obtain the last 12 records of type P
$result=mysql_query("select * from table where table.type='P' order by table.id DESC limit 12") or die(mysql_error());
// rest of your code

// obtain the last record of type M
$result=mysql_query("select * from table where table.type='M' order by table.id DESC limit 1") or die(mysql_error());
// rest of your code

Best regards,



Basically the script is for an picture/video album, so the numbers are not always the same, the code from DrK fixed my problem I was just too tired to see it skipping every 5

Reply

TavoxPeru
QUOTE(djXternal @ Nov 3 2006, 10:24 PM) *

Basically the script is for an picture/video album, so the numbers are not always the same, the code from DrK fixed my problem I was just too tired to see it skipping every 5

Ok, no problem.

Best regards,

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Similar Topics

Keywords : debugging


    Looking for debugging






*SIMILAR VIDEOS*
Searching Video's for debugging
advertisement




Need Help Debugging