EzwanAbid Posted December 16, 2012 Share Posted December 16, 2012 (edited) I'm trying to make an If Else Statement in my while loop which that statement can read the value from what the user inserted. Each numbers have their own link which process different type of letter. Is that possible to insert 'If ... Else Statement' in my While Loop ?? because I keep getting an error message when I combine If Else inside my While Loop. But without If Else , there's no error message appear. Hope from this code you can see what I'm trying to do . <?php $host_name = "localhost"; $user_name = "root"; $password = ""; $db_name = "finalproject"; $StudentID1=$_POST['Field0']; $StudentID2=$_POST['Field1']; $StudentID3=$_POST['Field2']; $term =$StudentID1.-$StudentID2.-$StudentID3; mysql_connect("$host_name" , "$user_name" , "$password"); mysql_select_db("$db_name"); $sql = "SELECT * FROM student,surat WHERE student.student_id like '%$term%' AND student.student_id=surat.student_id"; $query = mysql_query($sql) or die("Error: " . mysql_error() . "<br />In Query: " . $sql); while ($row = mysql_fetch_array($query)){ echo 'Student ID: '.$row['student_id']; echo '<br/> Fullname: '.$row['fullname']; echo '<br/> IC No.: '.$row['ic_number']; echo '<br/> Course: '.$row['course']; echo '<br/> Type Of Letter: '.$row['jenissurat']; echo '<br/><br/>'; $jenis=$rows['jenissurat'] if($jenis=='1') { ?> <a href="PHPWord/PengesahanBelajar-code.php">Generate Letter</a> <? } else if($jenis=="2") { ?> <a href="PHPWord/KebenaranProjek-code.php">Generate Letter</a> <? } else if($jenis=="3") { ?> <a href="PHPWord/PelepasanExam-code.php">Generate Letter</a> <? } else if($jenis=="4") ?> <a href="PHPWord/TamatBelajar-code.php">Generate Letter</a><? } } ?> I also need a help to bring data of '.$row['jenissurat']; as my If Else condition.. Many of this code I copy from a website.. I'm new in PHP and Mysql ... but I MUST to complete this code without error. I already download 2 file that have connection with this code. Hope someone can give me an advice,solution or corrections maybee.. Thank You. Search.php searchres.php Edited December 16, 2012 by EzwanAbid Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/ Share on other sites More sharing options...
DavidAM Posted December 16, 2012 Share Posted December 16, 2012 When you are going to drop out of PHP in an IF statement, you have to use the curly braces. You have done this everywhere except in the if ($jenis=="4") condition. So that one would throw an error. Looks like you put the closing brace in, so you are missing an open-brace. Rather than do a bunch of if-else statements, this might be easier to code and read if you use a switch statement and don't drop out of PHP. while ($row = mysql_fetch_array($query)){ echo 'Student ID: '.$row['student_id']; echo '<br/> Fullname: '.$row['fullname']; echo '<br/> IC No.: '.$row['ic_number']; echo '<br/> Course: '.$row['course']; echo '<br/> Type Of Letter: '.$row['jenissurat']; echo '<br/><br/>'; $jenis=$rows['jenissurat'] switch($jenis) { case "1": echo '<a href="PHPWord/PengesahanBelajar-code.php">Generate Letter</a>'; break; case "2": echo '<a href="PHPWord/KebenaranProjek-code.php">Generate Letter</a>'; break; case "3": echo '<a href="PHPWord/PelepasanExam-code.php">Generate Letter</a>'; break; case "4": echo '<a href="PHPWord/TamatBelajar-code.php">Generate Letter</a>'; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399712 Share on other sites More sharing options...
EzwanAbid Posted December 17, 2012 Author Share Posted December 17, 2012 Thank you so much for your reply. I already change my code just like you give me. But I get this error message .. Parse error: syntax error, unexpected T_SWITCH in D:\xampp\htdocs\Project\FinalProject\searchres.php on line 90 This is line 90 code : switch($jenis) { Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399776 Share on other sites More sharing options...
Christian F. Posted December 17, 2012 Share Posted December 17, 2012 Note that when you get code from a forum like this, the code is almost always just example code and not tested. Thus there might be bugs in it, or other stuff that's not tailored to the rest of your code. That's why you should never copy-paste the code given, but rather study it and use it as an example on how to write your own. In this case the error is pretty clear cut, and if you spend a couple of minutes looking at the preceding lines it should be fairly apparent. Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399808 Share on other sites More sharing options...
EzwanAbid Posted December 17, 2012 Author Share Posted December 17, 2012 Thank you for your reply and advice Christian F., I already try to figure it out but the only I get is error. That's why I need some help here. Sorry for that :-\ I mean for my condition of Switch Case.. how to bring the data from row/query into my condition .. ? Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399821 Share on other sites More sharing options...
Jessica Posted December 17, 2012 Share Posted December 17, 2012 Look at the line before 90. Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399826 Share on other sites More sharing options...
jotorres1 Posted December 17, 2012 Share Posted December 17, 2012 Just like jessica said. The line beofre 90. You are missing ';' add it to the end of line dude. $jenis=$rows['jenissurat']; Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399828 Share on other sites More sharing options...
EzwanAbid Posted December 17, 2012 Author Share Posted December 17, 2012 Oh My God !!! Christian F. you were right ! I miss the semicolon at line 90.. seriously, I'm staring at my code for more than half an hour and I found nothing until I noticed that I miss the semicolon there.. And thank you again for your advice ! I already know how to get data from my row and my problem are solved. Thank you for reply Jessica, I also know sometimes the error are not directed to that line.. Anyway, thank you again for replying my newbie post Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399832 Share on other sites More sharing options...
Christian F. Posted December 17, 2012 Share Posted December 17, 2012 Hehe, at least you'll remember it for sure now. Glad we could help. Quote Link to comment https://forums.phpfreaks.com/topic/272063-can-i-use-if-else-statement-in-my-while-loop/#findComment-1399858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.