Jump to content

mysql/php help


imarockstar

Recommended Posts

I have a DB with a crap load of data .. however some data fields are empty and when I want to display all the rows .. but if a certain data field is blank I do not want to display the header ... basically so theres not an empty spot on the page ...

 

my code for not displaying info if mysql returns no rows ..

 

if (mysql_num_rows($result) < 1)
{
  echo "You have no resumes for this job posting. ";
}

 

HOWVER ... what if I have variables .... data1 data2 data3 ...

 

data1 has info stored ...

data2 has NO INFO

data3 has info stored ..

 

in my code above how would i not display just data2 ?

 

make since .. kuz i just confused myself ...

 

 

 

 

 

 

Link to comment
Share on other sites

When you fetch a data , it is an array like this : 

 

array(0 => data1, 1 => data2, 3 => data3)

 

so you can use foreach to findout if they are empty or not.

 

while ( $rows = mysql_fetch_array($result))

 

{
  foreach ($rows as $vak)
{ 
   if ($val != "") { echo $val;}
}
}

 

edit  :

The real fetched array is like this : 

 

array(0 => data1,name_of_field1 => data1, 1 => data2,name_of_field2 => data2 , 3 => data3,name_of_field3 => data3)

 

you can access each value by a numeric key --> $rows[0] or by its field name --> $rows[name_of_field1] , both same.

 

notice the while loop, make it go for all the rows.

Link to comment
Share on other sites

sorry ...

 

 

<?

$id = $_GET["id"];

//select the table
$result = mysql_query("select * from jobs where id='$id' ");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $jobtitle =$r["jobtitle"];
   $jobid =$r["jobid"];
   $recid =$r["recid"];
   $jobdesc =$r["jobdesc"];
   $jobreq1 =$r["jobreq1"];
   $jobreq2 =$r["jobreq2"];
   $jobreq3 =$r["jobreq3"];
   $jobreq4 =$r["jobreq4"];
   $jobreq5 =$r["jobreq5"];
   $jobreq6 = $r['jobreq6'];
   $jobreq7 = $r['jobreq7'];
   $jobreq8 = $r['jobreq8'];
   $jobreq9 = $r['jobreq9'];
   $jobreq10 = $r['jobreq10'];
   $jobreq11 = $r['jobreq11'];
   $jobreq12 = $r['jobreq12'];
   $jobloc =$r["jobloc"];
   $jobtype =$r["jobtype"];
   $jobpay =$r["jobpay"];
   $jobtravel =$r["jobtravel"];
?>
   

   
   




<h2 class=><?=$jobtitle ?></h2>

<h3 class=>Job Number: <?=$jobid ?></h3>



<h4 class=jobdisplay>Job Description</h4>

<span class=jobdescription>
<?php echo nl2br($jobdesc); ?>
</span>

<br><br>


<h4 class=jobdisplay>Carreer Requirments</h4>



<ul class=jobdata>
<li class=jobdata><?=$jobreq1 ?></li>
<li class=jobdata><?=$jobreq2 ?></li>
<li class=jobdata><?=$jobreq3 ?></li>
<li class=jobdata><?=$jobreq4 ?></li>
<li class=jobdata><?=$jobreq5 ?></li>
<li class=jobdata><?=$jobreq6 ?></li>
<li class=jobdata><?=$jobreq7 ?></li>
<li class=jobdata><?=$jobreq8 ?></li>
<li class=jobdata><?=$jobreq9 ?></li>
<li class=jobdata><?=$jobreq10 ?></li>
<li class=jobdata><?=$jobreq11 ?></li>
<li class=jobdata><?=$jobreq12 ?></li>
</ul>



<h4 class=jobdisplay>Job Location</h4>
<span class=jobdata><?=$jobloc ?></span>

<br><br>

<h4 class=jobdisplay>Employment Type</h4>
<span class=jobdata><?=$jobtype ?></span>

<br><br>

<h4 class=jobdisplay>Pay Rate</h4>
<span class=jobdata><?=$jobpay ?></span>

<br><br>

<h4 class=jobdisplay>Travel Required</h4>
<span class=jobdata><?=$jobtravel ?></span>

<br><br>



<div class=applynow>
<a href="jobs_apply?jobid=<?=$jobid ?>&jobtitle=<?=$jobtitle ?>&recid=<?=$recid ?>">
<img src="images/apply_now.gif" alt="image" width="273" height="118" />
</a>
</div>			



<?php } ?>


 

 

this is my main concern .. .these are always left blank ...

 

how would i not display them if there was no data ..

 

<ul class=jobdata>
<li class=jobdata><?=$jobreq1 ?></li>
<li class=jobdata><?=$jobreq2 ?></li>
<li class=jobdata><?=$jobreq3 ?></li>
<li class=jobdata><?=$jobreq4 ?></li>
<li class=jobdata><?=$jobreq5 ?></li>
<li class=jobdata><?=$jobreq6 ?></li>
<li class=jobdata><?=$jobreq7 ?></li>
<li class=jobdata><?=$jobreq8 ?></li>
<li class=jobdata><?=$jobreq9 ?></li>
<li class=jobdata><?=$jobreq10 ?></li>
<li class=jobdata><?=$jobreq11 ?></li>
<li class=jobdata><?=$jobreq12 ?></li>
</ul>

 

 

 

 

 

 

 

Link to comment
Share on other sites

Put your while loop where you are echoing the list . AND you don't really need to put each $r[] variable in a new variable  :

 

echo "<ul>";
while($r=mysql_fetch_array($result))
{

foreach($r as $val)
{
if ($val != "")
{
echo "<li class=jobdata>$jobreq1</li>";
}}}

echo "</ul>";

Link to comment
Share on other sites

sorry i meant the one directly above .. here is my code ..

 

none of the job requirements are showing up .. blah

 

<?

$id = $_GET["id"];

//select the table
$result = mysql_query("select * from jobs where id='$id' ");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $jobtitle =$r["jobtitle"];
   $jobid =$r["jobid"];
   $recid =$r["recid"];
   $jobdesc =$r["jobdesc"];
   $jobreq1 =$r["jobreq1"];
   $jobreq2 =$r["jobreq2"];
   $jobreq3 =$r["jobreq3"];
   $jobreq4 =$r["jobreq4"];
   $jobreq5 =$r["jobreq5"];
   $jobreq6 = $r['jobreq6'];
   $jobreq7 = $r['jobreq7'];
   $jobreq8 = $r['jobreq8'];
   $jobreq9 = $r['jobreq9'];
   $jobreq10 = $r['jobreq10'];
   $jobreq11 = $r['jobreq11'];
   $jobreq12 = $r['jobreq12'];
   $jobloc =$r["jobloc"];
   $jobtype =$r["jobtype"];
   $jobpay =$r["jobpay"];
   $jobtravel =$r["jobtravel"];
?>
   

   
   




<h2 class=><?=$jobtitle ?></h2>

<h3 class=>Job Number: <?=$jobid ?></h3>



<h4 class=jobdisplay>Job Description</h4>

<span class=jobdescription>
<?php echo nl2br($jobdesc); ?>
</span>

<br><br>


<h4 class=jobdisplay>Carreer Requirments</h4>


<?php
echo "<ul>";
while($r=mysql_fetch_array($result))
{

foreach($r as $val)
{
if ($val != "")
{
echo "<li class=jobdata>$jobreq1</li>";
}}}

echo "</ul>";
?>


<h4 class=jobdisplay>Job Location</h4>
<span class=jobdata><?=$jobloc ?></span>

<br><br>

<h4 class=jobdisplay>Employment Type</h4>
<span class=jobdata><?=$jobtype ?></span>

<br><br>

<h4 class=jobdisplay>Pay Rate</h4>
<span class=jobdata><?=$jobpay ?></span>

<br><br>

<h4 class=jobdisplay>Travel Required</h4>
<span class=jobdata><?=$jobtravel ?></span>

<br><br>



<div class=applynow>
<a href="jobs_apply?jobid=<?=$jobid ?>&jobtitle=<?=$jobtitle ?>&recid=<?=$recid ?>">
<img src="images/apply_now.gif" alt="image" width="273" height="118" />
</a>
</div>			



<?php } ?>

 

 

 

Link to comment
Share on other sites

remove this :

 

while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $jobtitle =$r["jobtitle"];
   $jobid =$r["jobid"];
   $recid =$r["recid"];
   $jobdesc =$r["jobdesc"];
   $jobreq1 =$r["jobreq1"];
   $jobreq2 =$r["jobreq2"];
   $jobreq3 =$r["jobreq3"];
   $jobreq4 =$r["jobreq4"];
   $jobreq5 =$r["jobreq5"];
   $jobreq6 = $r['jobreq6'];
   $jobreq7 = $r['jobreq7'];
   $jobreq8 = $r['jobreq8'];
   $jobreq9 = $r['jobreq9'];
   $jobreq10 = $r['jobreq10'];
   $jobreq11 = $r['jobreq11'];
   $jobreq12 = $r['jobreq12'];
   $jobloc =$r["jobloc"];
   $jobtype =$r["jobtype"];
   $jobpay =$r["jobpay"];
   $jobtravel =$r["jobtravel"];

 

and change

echo "<ul>";
while($r=mysql_fetch_array($result))
{

foreach($r as $val)
{
if ($val != "")
{
echo "<li class=jobdata>$jobreq1</li>";
}}}

echo "</ul>";

 

to

 

echo "<ul>";
while($r=mysql_fetch_array($result))
{

foreach($r as $val)
{
if ($val != "")
{
echo "<li class=jobdata>$val</li>";
}}}

echo "</ul>";

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.