Jump to content

(!empty) is not working on desire code


genzedu777

Recommended Posts

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]

Link to comment
Share on other sites

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'].', ';
            }
         }
?>

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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?

 

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.