Jump to content

Why My While Loop Looping Twice ?


EzwanAbid

Recommended Posts

Aw I never I found another new error because I though I already fix them all..

So here my problem, my while loop is looping twice..

 

They exactly display the data that I want, but they will display it twice.. so that's my problem.

 

here my code :

 

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="finalproject"; // Database name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM student,surat,admin WHERE student.student_id=surat.student_id";
$result=mysql_query($sql);

?>
<link href="TableCSSCodeupdate.css" rel="stylesheet" type="text/css">

<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<table width="612" border="0" cellspacing="2" cellpadding="0" class="CSSTableGenerator">

<tr align="center">
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<td align="left"></td>
<td align="left"><div align="left"><b>NO.</b> <td>:</td><td><? echo $rows['id_surat']; ?></div></td></tr>
<td align="left"></td>
<td align="left"><div align="left"><b>NAME</b> <td>:</td><td><? echo $rows['fullname']; ?></div></td></tr>
<td align colspan="1" rowspan="2"></td>
<tr align="left">
<td align="left"><div align="left"><b>STUDENT ID</b> <td>:</td><td> <? echo $rows['student_id']; ?></div></td></tr>
<td align colspan="1" rowspan="2"></td>
<tr align="left">
<td align="left"><div align="left"><b>IC NO.</b> <td>:</td><td><? echo $rows['ic_number']; ?></div></td></tr>
<td align colspan="1" rowspan="2"></td>
<tr align="left">
<td align="left"><div align="left"><b>COURSE</b> <td>:</td><td><? echo $rows['course']; ?></div></td></tr>
<td align colspan="1" rowspan="2"></td></tr>
<tr align="left">
<td align="left"><div align="left"><b>TYPE OF LETTER</b><td>:</td><td><? echo $rows['jenissurat']; ?></div></td></tr>
<tr>
<th scope="col"> </th>
<th scope="col"> </th>
<th scope="col"> </th>
<th scope="col"> </th>
</tr>
<tr align="left">
<th scope="col"> </th>
<th scope="col">

<?php
$ids=$rows['id_surat'];
$jenis=$rows['jenissurat'];


switch($jenis) {
case "TamatBelajar":
echo '<a href="PHPWord/TamatBelajar-code.php">Generate Letter</a><br><br>';
 mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'");
break;


case "PengesahanBelajar":
echo '<a href="PHPWord/PengesahanBelajar-code.php">Generate Letter</a><br><br>';
 mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'");
break;


case "KebenaranProjek":
echo '<a href="PHPWord/KebenaranProjek-code.php">Generate Letter</a><br><br>';
 mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'");
break;


case "PelepasanPeperiksaanAkhir":
echo '<a href="PHPWord/PelepasanExam-code.php">Generate Letter</a><br><br>';
 mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'");
break;
}?>

</tr>
<tr>
<th scope="col"> </th>
<th scope="col"> </th>
<th scope="col"> </th>
<th scope="col"> </th>
</tr>
</tr>

<?php
}
?>

</table>

</td>
</tr>
</table>
<br>

<?php
mysql_close();
?>


</body>
</html>

 

I already download the file for that contain the full code.

I really need help and solution ASAP.. Please :'(

QueueLetterForm.php

Link to comment
Share on other sites

@CPD

Do a var_dump on the $results variable and you'll likely see you have dupes meaning you need to review your SQL as its doubling up the data.

 

$results does not exist in that script. $result is a mysql resource and var_dump'ing it will not show the duplicate rows. Other than that, you are on the right track.

 

@EzwanAbid

$sql="SELECT * FROM student,surat,admin WHERE student.student_id=surat.student_id";

 

You have three tables in that query, but only one "join" condition. So every row that matches student and surat will be matched with every row in the admin table (a Cartesian product). My guess is you have two rows in that table. You need to fix the query to fix your problem.

Link to comment
Share on other sites

Thank for your reply..

How to make those var_dump? I already try to learn from certain tutorial about var_dump.. and I not really understand how to do it.

 

 

@EzwanAbid

 

 

You have three tables in that query, but only one "join" condition. So every row that matches student and surat will be matched with every row in the admin table (a Cartesian product). My guess is you have two rows in that table. You need to fix the query to fix your problem.

 

Yes, I only need to 'join up' student and surat table which the data I want is only student_id.. the others data from admin and surat table I don't think I need to combine both of them because I need to display almost all of the data inside both table.

Link to comment
Share on other sites

I think you need to learn more about SQL, and databases in general. I don't see any reason for the while loop to run twice. In fact, it is not running twice. The problem is that you are retrieving the data "twice". If you are not using data from the admin table, then take it out of the query. If you are using data from the table, then you need to join that table to one of the other tables in the query. As it is, you are getting a Cartesian product, which will cause the output to look like you are printing each row twice, when in fact you are retrieving all of the data twice.

 

var_dump is a PHP function that prints the contents and data-type of a variable. It is a useful tool for debugging when you want to see what a variable contains. As I stated earlier, it is not going to help you in this case; 1) $result is a resource not all of the data from the query, and 2) the problem is not with any of the variables, the problem is your query.

 

Try running that query directly in mysql and see what the output is.

Link to comment
Share on other sites

Thank you for replying DavidAM,

 

ok I try this code

var_dump ($result);

 

and after that I get this output

resource(4) of type (mysql result) resource(4) of type (mysql result)

 

can someone tell me the correct code to "join the condition" like

$sql="SELECT * FROM student,surat,admin WHERE student.student_id=surat.student_id";

 

actually, I'm just a newbie in PHP and MySQL .. some of the code above I take from website and some I make it myself base on tutorials.

I don't understand the code "student.student_id=surat.student_id" .. what is the meaning of this code?

Link to comment
Share on other sites

the result I want is to get data and display :

 

Table : admin
Column needed : adminfullname , department


Table : student
Column needed : student_id, fullname, ic_number, course,


Table : surat
Column needed : id_surat, jenissurat

 

What I'm trying to do here is to display all the data that waiting for the "Letter Process" .. This system is about the "Letter Generator System" which it will bring the data from database and send into Microsoft Word template to be processes.

Link to comment
Share on other sites

As far as I can tell there is no relationship between admin and the other two tables (hence Christian's question) and as such you would need a minimum of two queries to retrieve data from both.

 

Yes but still I need data from admin table .. so I need another query?

Seriously I'm not that good in MySQL code..

so can I use this 1 and just insert it into my code?

 

$newsql=("SELECT *FROM admin WHERE adminfullname,department");
$newresult=mysql_query($newsql);

Link to comment
Share on other sites

That query doesn't make sense. If you use a WHERE clause you must give conditions of which you've given none:

 

"SELECT * FROM `admin` WHERE `adminfullname` = 'Something' AND `department` = 'Something'"

 

As for the other query you can do something like:

 

"SELECT * FROM `student` `st` LEFT JOIN `surat` `su` ON `su`.`student_id` = `st`.`student_id`"

Edited by CPD
Link to comment
Share on other sites

 

As for the other query you can do something like:

 

"SELECT * FROM `student` `st` LEFT JOIN `surat` `su` ON `su`.`student_id` = `st`.`student_id`"

 

Thank you for your help. but

did you just set a new variable/name for student as 'st' ? and surat as 'su'? in that query? Why must we do so?

Link to comment
Share on other sites

Yes ! They no longer display the result twice..

Thank you for teach me guy... Now I've already learn how use var_dump and some knowledge about SQL Join Query ...

Thank you very much ! It really working and I'm so happy you guys can help me ASAP.. I'm really appreciate that because I need to get this work done less in 4 days. .The problem is SOLVED. !

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.