imarockstar Posted October 1, 2008 Share Posted October 1, 2008 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 https://forums.phpfreaks.com/topic/126671-mysqlphp-help/ Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 It would help to see your code, but I would say with an IF statement. Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655101 Share on other sites More sharing options...
volatileboy Posted October 1, 2008 Share Posted October 1, 2008 I didnt quite understand what you were propperly saying but something like this would only output something that wasn't empty if(empty($row->data1)) { echo $row->data1; } Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655102 Share on other sites More sharing options...
volatileboy Posted October 1, 2008 Share Posted October 1, 2008 Correction: if(!empty($row->data1)) { echo $row->data1; } Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655105 Share on other sites More sharing options...
asmith Posted October 1, 2008 Share Posted October 1, 2008 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 https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655108 Share on other sites More sharing options...
imarockstar Posted October 1, 2008 Author Share Posted October 1, 2008 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 https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655112 Share on other sites More sharing options...
volatileboy Posted October 1, 2008 Share Posted October 1, 2008 Try: { foreach ($rows as $val) { if (!empty($val)) { echo $val;} } } Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655113 Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 Replace each <li> line with this: <?php if (!empty($jobreq1) echo "<li class=jobdata>$jobreq1</li>"; ?> Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655114 Share on other sites More sharing options...
volatileboy Posted October 1, 2008 Share Posted October 1, 2008 Just a pointer, can't be bothered doing all of it lol, you could do this: Instead of just putting this: <li class=jobdata><?=$jobreq1 ?></li> try this: if(!empty($jobreq1)) { echo '<li class=jobdata>' . $jobreq1 . '</li>' } Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655116 Share on other sites More sharing options...
asmith Posted October 1, 2008 Share Posted October 1, 2008 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 https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655120 Share on other sites More sharing options...
imarockstar Posted October 1, 2008 Author Share Posted October 1, 2008 I did the above and it displays nothing .. Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655143 Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 Which one? You've had a lot of options. Also, re-post your code now that you've changed it. Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655147 Share on other sites More sharing options...
imarockstar Posted October 2, 2008 Author Share Posted October 2, 2008 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 https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655556 Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Is you query supposed to return one row, or multiple rows? Link to comment https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-655655 Share on other sites More sharing options...
asmith Posted October 3, 2008 Share Posted October 3, 2008 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 https://forums.phpfreaks.com/topic/126671-mysqlphp-help/#findComment-656142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.