Jump to content

Recommended Posts

Hi i was wondering if anyone can help me with this.

 

I'm using the while loop to get multiple rows of information from a table called ani_character with a common id - in this case anime_id where the table has all the character information.

When i run it along with a query to get information from another table - anime where information about the show is stored. It gets the information i need but it seems to skip the first row and display all the others after that.

 

So if there are four characters linked to the show it will only display the 2nd -- to the last row and not the first. How can i tell the while command to start from the first row found and loop from there and also is there a better way to display with the while command than having to echo everything.

 

include('includes/conn.inc.php');
$que = "SELECT * FROM ani_character WHERE anime_id=$id";
// Execute the query
$res = mysql_query( $que );
if (!$res){
die ("Could not query the database: <br />". mysql_error( ));
}
// Fetch and display the result


while ($result_row = mysql_fetch_assoc(($res))){
echo    '<div id="content_character">';
echo    '<div id="cha_img">' . '<img src=" ' .$result_row[ac_img] . '" width="120"/>' . '</div>';
echo    '<div id="cha_right">' .$result_row[ac_name] . '</div>';
echo    '<hr id="m2"/>';
echo    '<div id="cha_rightvj">' . 'Voice Jap : ' .$result_row[ac_jap] . '</div>';
echo    '<div id="cha_rightve">' . 'Voice Eng : ' .$result_row[ac_eng] . '</div>';
echo    '</div>';
} 

 

thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/240690-while-loop/
Share on other sites

View the HTML source from the screen. Are you sure it is not there? Run the query in phpmyadmin or whatever you manage your database with to see what results are returned:

SELECT * FROM ani_character WHERE anime_id=$id

Simple tests, but a while loop will not exclude data.

Link to comment
https://forums.phpfreaks.com/topic/240690-while-loop/#findComment-1236236
Share on other sites

okay i will check it again but when i run the query in a separate file and include it with the include command it works fine but if i join it up on the same page with a previous query that is when it skips the first row.

 

this is basically how the database is set up. except for the id for the anime table is 1 and the a_chid is now anime_id and is also 1 for those.

 

what i was doing before was running a query between the two tables to select the information where the id = 1 and told it that the anime and ani_character id's where linked ie. anime.id=ani_character.anime_id AND id=1

 

and i was using the and command to select the show as above but when i load the page like i said i only get the information after the first row. I don't know if maybe the query is using the first row to join them together or what but when i run the query in mysql console i get all the information i requested, and like i said when i use the include() it works fine.

 

is there any other way to use a loop command like while and is there an easier way to display information with it besides having to echo the tables and things like that.

 

thanks

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/240690-while-loop/#findComment-1236242
Share on other sites

what i was doing before was running a query between the two tables to select the information where the id = 1 and told it that the anime and ani_character id's where linked ie. anime.id=ani_character.anime_id AND id=1

 

and i was using the and command to select the show as above but when i load the page like i said i only get the information after the first row. I don't know if maybe the query is using the first row to join them together or what but when i run the query in mysql console i get all the information i requested, and like i said when i use the include() it works fine.

 

This really doesn't make sense. Are you posting the entire code? I can only see 1 query. I cannot see where $id is set. If you want to join more than one table you use the JOIN syntax in your query. You do not run sub queries!

 

is there any other way to use a loop command like while and is there an easier way to display information with it besides having to echo the tables and things like that.

 

A while loop is a while loop regardless of the language, php, asp, c, etc There are also for loops, foreach loops, do while loops, however a while loop is fine.

As for the second point, how else would you display data in a web browser? The HTML has to exist. The echo command prints the string of HTML containing your dynamic data from your database query. You need a loop if there is more than 1 record retuned from a query.

Link to comment
https://forums.phpfreaks.com/topic/240690-while-loop/#findComment-1236261
Share on other sites

Yeah sorry i don't have it on me as im doing it at home and am at work at the moment.

 

the query im using at the start of the page is

 

SELECT anime.*, ani_character.* FROM anime, ani_character WHERE anime.id=ani_character.anime_id AND anime.id=1 

 

and then i have it display the information for the show etc...

 

and beneath that i run the while statement. Whether i use another query or as i have done there or use the original one above i only get the results from the 2nd row not the 1st onwards.

 

include('includes/conn.inc.php');
$que = "SELECT * FROM ani_character WHERE anime_id=$id";
// Execute the query
$res = mysql_query( $que );
if (!$res){
die ("Could not query the database: <br />". mysql_error( ));
}
// Fetch and display the result


while ($result_row = mysql_fetch_assoc(($res))){
echo    '<div id="content_character">';
echo    '<div id="cha_img">' . '<img src=" ' .$result_row[ac_img] . '" width="120"/>' . '</div>';
echo    '<div id="cha_right">' .$result_row[ac_name] . '</div>';
echo    '<hr id="m2"/>';
echo    '<div id="cha_rightvj">' . 'Voice Jap : ' .$result_row[ac_jap] . '</div>';
echo    '<div id="cha_rightve">' . 'Voice Eng : ' .$result_row[ac_eng] . '</div>';
echo    '</div>';
}  

 

i know it has to print the html i was just wondering if there was an easier way of using the while command in regards to printing it, but i'll managed with it.

Link to comment
https://forums.phpfreaks.com/topic/240690-while-loop/#findComment-1236288
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.