...

View Full Version : While loop help plz



PRodgers4284
03-03-2008, 08:05 PM
I have problem displaying multiple records from a mysql database, the code wont display anything, can anyone please help?

My code is


<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location: index2.php");
}

include("database.php");
include("loginemployer.php");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jobs4U</title>


<!--CSS Stylesheet -->
<link rel="stylesheet" type="text/css" href="./css/main.css" />
<link rel="stylesheet" type="text/css" href="./css/registerform.css" />
</head>

<body style="margin-left:1px;margin-right:1px;margin-bottom:0px;margin-top:0px;">

<!--Main Container -->
<div id="container">

<!--Main Header -->
<div id="header" style="width: 988px; height: 154px">
&nbsp;
</div>


<!-- Navigation Menu -->
<div id="nav_menu">
<div class="link_container" style="width: 852px; height: 32px">
<a href="Index.php" class="main_nav">home</a><span class="link_separator">::</span>
<a href="page.htm" class="main_nav">products</a><span class="link_separator">::</span>
<a href="page.htm" class="main_nav">services</a><span class="link_separator">::</span>
<a href="page.htm" class="main_nav">about us</a><span class="link_separator">::</span>
<a href="page.htm" class="main_nav">contact</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="search0" maxlength="30" size="30" >&nbsp;&nbsp;
<a href="page.htm" class="main_nav">Quick Search&gt;&gt;&gt;</a></div>
</div>

<!-- Container (left) -->
<div id="body">
<div id="body-left-panel" style="width: 654px; height: 811px">
<div style="padding:10px;">

<!-- Left container 1(Top) -->
<div class="box_upper_partviewdetails">
<img src="img/box_up-left_edge.png" alt="" class="align-left"/>
<img src="img/box_up-right_edge.png" alt="" class="align-right"/>
<span class="box_header">:: Company Account Details</span></div>

<!--(Middle) -->
<div class="box_middle_partviewdetails">
<div class="pad_15tah">

<!--Register Form -->

<?php


$q = mysql_fetch_array(mysql_query("SELECT * FROM job WHERE username='" . $_SESSION["username"] . "'"));
while ($account = @mysql_fetch_array($q))
{

$id=$account["id"];
$jobtitle=$account["jobtitle"];



echo "<table>";
echo "<tr><td>$jobtitle</td></tr>";
echo "</table>";

}
?>
<!--this will show whatever is in the $message variable -->
</div>
</div>



<!-- (bottom) -->
<div class="box_lower_part">
<img src="img/box_low-left_edge.png" alt="" class="align-left"/>
<img src="img/box_low-right_edge.png" alt="" class="align-right"/>
</div>

<!--Left Container 2 (Top) -->
</div>
</div>
<p>

<!--Right Panel 1 -->
</p>
<div id="body-right-panel" style="width: 333px; height: 811px">
<div style="padding:10px;">


<!--Top -->
<div class="box_upper_part">
<img src="img/box_up-left_edge_dark.png" alt="" class="align-left"/>
<img src="img/box_up-right_edge_dark.png" alt="" class="align-right"/>
<span class="box_header">Employer Login</span></div>


<!--Middle -->
<div class="box_middle_part">
<div style="padding:5px;">
<ul class="img_red">


<!--Login -->
<?php displayLoginemployer(); ?>
<p><br /></p>
</div>
</div>


<!-- (bottom) -->
<div class="box_lower_part">
<img src="img/box_low-left_edge_dark.png" alt="" class="align-left"/>
<img src="img/box_low-right_edge_dark.png" alt="" class="align-right"/>
</div>


<!--Right Panel 2 (top) -->
<div class="box_upper_part" style="width: 313px; height: 29px">
<img src="img/box_up-left_edge_dark_nodot.png" alt="" class="align-left"/>
<img src="img/box_up-right_edge_dark.png" alt="" class="align-right"/>
<span class="box_header">:: Resource Centre</span>
</div>


<!--Middle -->
<div class="box_middle_part">
<div style="padding:10px;">
<div align="center">
<table border="0" width="65%" id="table12" cellspacing="0" cellpadding="0" bgcolor="#E7E6E4">
<tr>
<td>
<img border="0" src="images/cv.gif" width="221" height="23"></td>
</tr>
<tr>
<td><p>&nbsp;</p>
<p><span class="navyboldtxt">CV Advice</span>. </p>
<p>&nbsp;- <a href="cvadvice.php">How to create a CV</a> </p>
<p>&nbsp;- <a href="cvreferences.php">CV References</a> </p>
<p>&nbsp;</p>
</td>
</tr>
</table>
<table border="0" width="65%" id="table13" cellspacing="0" cellpadding="0" bgcolor="#E7E6E4">
<tr>
<td>
<img border="0" src="images/interview.gif" width="221" height="23">
</td>
</tr>
<tr>
<td>&nbsp;<p><span class="navyboldtxt">
Interview Advice</span> - </p>
<p>&nbsp;&nbsp;- <a href="interviewadvice.php">Interview tips</a> </p>
<p>&nbsp;</p><p>&nbsp;</td>
</tr>
</table>
</div>
</div>
</div>


<!-- (bottom) -->
<div class="box_lower_part" style="width: 313px; height: 22px">
<img src="img/box_low-left_edge_dark.png" alt="" class="align-left"/>
<img src="img/box_low-right_edge_dark.png" alt="" class="align-right"/>
</div>
</div>
</div>
</div>


<!-- Footer -->
<div id="footer">
<div id="footer_designby" style="width: 987px; height: 30px">
<div class="designedby">Designed by
<a href=""></a></div>
</div>
</div>
</div>

</body>
</html>

PRodgers4284
03-03-2008, 09:06 PM
I am using the following while loop, which looks up a table "job" and checks the username session of the person is logged in, the code is then supposed loop round each record and display the jobtitle of each job in the table assigned to that user. The code displays nothing when i run it,
can someone please help, i dont know what the problem is.


<?php


$q = mysql_fetch_array(mysql_query("SELECT * FROM job WHERE username='" . $_SESSION["username"] . "'"));
while ($account = @mysql_fetch_array($q))
{

$id=$account["id"];
$jobtitle=$account["jobtitle"];



echo "<table>";
echo "<tr><td>$jobtitle</td></tr>";
echo "</table>";

}
?>

oesxyl
03-03-2008, 09:30 PM
<?php

$sessid = $_SESSION["username"] ;
// look to the value if is what you expect
print $sessid;

$query = "SELECT * FROM job WHERE username='" . $sessid. "'";
// look if the query have valid mysql syntax
print $query;

$result = mysql_query($query);
if(!$result){
// check if is something wrong
print "this don't work";
}else{
if($mysql_numrows($result) == 0){
print "no data";
}else{
while ($account = @mysql_fetch_array($result)) {
$id=$account["id"];
$jobtitle=$account["jobtitle"];

echo "<table>";
echo "<tr><td>$jobtitle</td></tr>";
echo "</table>";
}
}
}

?>

PRodgers4284
03-03-2008, 09:41 PM
<?php

$sessid = $_SESSION["username"] ;
// look to the value if is what you expect
print $sessid;

$query = "SELECT * FROM job WHERE username='" . $sessid. "'";
// look if the query have valid mysql syntax
print $query;

$result = mysql_query($query);
if(!$result){
// check if is something wrong
print "this don't work";
}else{
if($mysql_numrows($result) == 0){
print "no data";
}else{
while ($account = @mysql_fetch_array($result)) {
$id=$account["id"];
$jobtitle=$account["jobtitle"];

echo "<table>";
echo "<tr><td>$jobtitle</td></tr>";
echo "</table>";
}
}
}

?>


Hi oesxyl thanks for the reply, i tried the above code but im getting the following error:

userSELECT * FROM job WHERE username='user'
Fatal error: Function name must be a string in C:\wamp\www\Jobs4U\viewjob.php on line 79

Sames to be the line:


if($mysql_numrows($result) == 0){



Im not sure whats happening here.

oesxyl
03-03-2008, 09:47 PM
hi,

sorry, is my fault, I don't test it and I misspell the name of the function. Instead of this:




if($mysql_numrows($result) == 0){

Im not sure whats happening here.

must be:

if($mysql_num_rows($result) == 0){


best regards

Dfraz
03-03-2008, 09:54 PM
It also must have a " } " at the end of your code.

PRodgers4284
03-03-2008, 10:04 PM
It also must have a " } " at the end of your code.

At the very end of the php code?

oesxyl
03-03-2008, 10:13 PM
At the very end of the php code?



<?php

if(!$result){
}else{
if($mysql_numrows($result) == 0){
print "no data";
}else{
while ($account = @mysql_fetch_array($result)){
....
}
}
}


thank you for replay, but, I don't see where it is.

best regards

oesxyl
03-03-2008, 10:35 PM
I hope that is final correction, I mess this line twice:


if($mysql_numrows($result) == 0){


- first missing a _
- second have a additional $

it must be:


if(mysql_num_rows($result) == 0){


I don't find any unclosed brackets but Dfraz or PRodgers4284 could
confirm or not if I'm right.

best regards

PRodgers4284
03-03-2008, 10:57 PM
I hope that is final correction, I mess this line twice:


if($mysql_numrows($result) == 0){


- first missing a _
- second have a additional $

it must be:


if(mysql_num_rows($result) == 0){


I don't find any unclosed brackets but Dfraz or PRodgers4284 could
confirm or not if I'm right.

best regards

This is correct, the problem was nothin to do with unclosed brackets



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum