genzedu777 Posted January 24, 2011 Share Posted January 24, 2011 Hi all, I have a situation here. Currently when a user register himself using our registration form, and did not select any subjects under 'Pre-School' level, the header still appears while retrieving out their details via web, rightfully speaking, it should not show any values, not even the 'Pre-School' header. However the situation now is, even if the user did not make any selections, the header of 'Pre-School' will still appear. How do I code in such if there is no value in Pre-School, it shouldn't even show the header? Should I use (!empty) command? Below is my (!empty) command code, it did not work. All suggestions are greatly welcome! Error Code - (!empty) command //Pre-School echo '<span class="row_header">Pre-School</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if (!empty($row3['level_id'] == 1)) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; Current complete workable code, without (!empty) command, however the 'Pre-School' header still appears <?php //Pre-School echo '<span class="row_header">Pre-School</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 1) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); //Lower Primary echo '<span class="row_header">Lower Primary</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 2) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); // Move internal result pointer back to first record. //Upper Primary echo '<span class="row_header">Upper Primary</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 3) { echo ''.$row3['subject_name'].', '; } } ?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
litebearer Posted January 24, 2011 Share Posted January 24, 2011 Just a thought (psuedo code) Perhaps using GROUP BY, then if(=1){ display head 1 loop thru group 1 } if(=2) { display head 2 loop thru group 2 } etc etc Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted January 24, 2011 Author Share Posted January 24, 2011 Hi Litebearer, Thank you for your response, but could you elaborate more on my codes? I'm still a little blur <?php //Pre-School echo '<span class="row_header">Pre-School</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 1) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); //Lower Primary echo '<span class="row_header">Lower Primary</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 2) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); // Move internal result pointer back to first record. //Upper Primary echo '<span class="row_header">Upper Primary</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 3) { echo ''.$row3['subject_name'].', '; } } ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted January 24, 2011 Share Posted January 24, 2011 please display your query Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks, this is my query <?php $query3 = "SELECT sl.level_id AS level_id, sl.subject_id, tl.level_name AS level_name, ts.subject_name AS subject_name " . "FROM tutor_overall_level_subject AS ols " . "INNER JOIN tutor_subject_level AS sl USING (subject_level_id) " . "INNER JOIN tutor_level AS tl USING (level_id) " . "INNER JOIN tutor_subject AS ts USING (subject_id) " . "WHERE ols.tutor_id = '" . $_GET['tutor_id'] . "'"; $data3 = mysqli_query($dbc, $query3) or die(mysqli_error($dbc)); if (mysqli_num_rows($data3) > 0) { echo '<div id="panel4">'; //Pre-School echo '<span class="row_header">Pre-School</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 1) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); //Lower Primary echo '<span class="row_header">Lower Primary</span><br/>'; $separator = ''; while($row3 = mysqli_fetch_array($data3)) { if($row3['level_id'] == 2) { echo $separator.$row3['subject_name']; $separator = ', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); // Move internal result pointer back to first record. //Upper Primary echo '<span class="row_header">Upper Primary</span><br/>'; $separator = ''; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 3) { echo $separator.$row3['subject_name']; $separator = ', '; } } echo '</div>'; //End of panel 4 } //End of IF for $query and $data (Teaching Subjects) else { echo '<p class="error">There was a problem accessing your profile.</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted January 24, 2011 Share Posted January 24, 2011 try replacing this... if (mysqli_num_rows($data3) > 0) { echo '<div id="panel4">'; //Pre-School echo '<span class="row_header">Pre-School</span><br/>'; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 1) { echo ''.$row3['subject_name'].', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); //Lower Primary echo '<span class="row_header">Lower Primary</span><br/>'; $separator = ''; while($row3 = mysqli_fetch_array($data3)) { if($row3['level_id'] == 2) { echo $separator.$row3['subject_name']; $separator = ', '; } } echo '<br/><br/>'; mysqli_data_seek($data3, 0); // Move internal result pointer back to first record. //Upper Primary echo '<span class="row_header">Upper Primary</span><br/>'; $separator = ''; while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 3) { echo $separator.$row3['subject_name']; $separator = ', '; } } echo '</div>'; //End of panel 4 with this... if(mysqli_num_rows($data3) > 0) { echo '<div id="panel4">'; while($row3 = mysqli_fetch_array($data3)) { if($row3['level_id'] == 1) { $preschool = $preschool .$row3['subject_name'].', ';} if($row3['level_id'] == 2) { $lowerprimary = $lowerprimary .$row3['subject_name'].', ';} if($row3['level_id'] == 3) { $upperprimary = $upperprimary .$row3['subject_name'].', ';} } if(strlen(trim($preschool))>0) { $preschool = substr($preschool,0,-2); echo '<span class="row_header">Pre-School</span><br/>'; echo $preschool . "<br/><br>"; } if(strlen(trim($lowerprimary))>0) { $lowerprimary = substr($lowerprimary,0,-2); echo '<span class="row_header">Lower Primary</span><br/>'; echo $lowerprimary . "<br/><br>"; } if(strlen(trim($upperprimary))>0) { $upperprimary = substr($upperprimary,0,-2); echo '<span class="row_header">Upper Primary</span><br/>'; echo $lowerprimary . "<br/><br>"; } echo '</div>'; //End of panel 4 Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted January 25, 2011 Author Share Posted January 25, 2011 Hi Litebearer, May I check with you, something regarding the codes. Just want to understand, why is there a need to count & Trim the num in the string using 'strlen' & 'trim'? if (strlen(trim($preschool) > 0))[img][/img] And why use substr? $preschool (substr($preschool, 0, -2); Thanks Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted January 25, 2011 Author Share Posted January 25, 2011 Hi Litebearer, After figuring out, I finally understand why use 'substr' This is to remove the 'comma' and the 'space' right? $preschool = substr($preschool,0,-2); How about strlen(trim)? why do we need to strlen and trim it? I realised if I were to remove 'strlen and trim', values from Pre-School will not appear in the website if(strlen(trim($preschool))>0) Thanks Quote Link to comment Share on other sites More sharing options...
litebearer Posted January 25, 2011 Share Posted January 25, 2011 yes, substr is to remove the trailing coma and space. You are right the trim portion is not needed, but keep strlen Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted January 25, 2011 Author Share Posted January 25, 2011 Hi Litebearer, It is really a good code, thanks, I have learnt so much from this. Lastly, I have a slight minute issue here. If you look at my attachment, you can see that right after subjects of 'Upper Primary', I have 2 <br><br> space, due to the fact that we have set <br><br> right after each level. How do I get rid off the spacing? Thanks if(strlen(trim($preschool))>0) { $preschool = substr($preschool,0,-2); echo '<span class="row_header">Pre-School</span><br/>'; echo $preschool . '<br/><br>'; } if(strlen(trim($lowerprimary))>0) { $lowerprimary = substr($lowerprimary,0,-2); echo '<span class="row_header">Lower Primary</span><br/>'; echo $lowerprimary . '<br/><br>'; } if(strlen(trim($upperprimary))>0) { $upperprimary = substr($upperprimary,0,-2); echo '<span class="row_header">Upper Primary</span><br/>'; echo $upperprimary . '<br/><br>'; } [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
litebearer Posted January 25, 2011 Share Posted January 25, 2011 Well being as Upper Primary is the last element to display, you can totally remove the <br>'s from its section; however, 7 possibile combos - preschool, lower and upper; preschool and lower; preschool and upper; preschool; lower and upper; lower; upper; in the Preschool section you need to add an if statement that check to see if either lower OR upper has value - if so display the <br>'s; then in the Lower section you need to see if upper has value - if so display the <br>'s; finally, no <br>'s need to be in the Upper section Make sense? Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted January 25, 2011 Author Share Posted January 25, 2011 Okay! It makes sense. I will go and try it out Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.