Jump to content

Can I Use If Else Statement In My While Loop?


EzwanAbid

Recommended Posts

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 by EzwanAbid
Link to comment
Share on other sites

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;
   }
}

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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 :happy-04:

Link to comment
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.