Jump to content

IF command query doesn't work


genzedu777

Recommended Posts

Hi all,

 

Perhaps, let me briefly explain what I would like to achieve.

 

Currently I am using INNER JOIN command to link 4 sql tables

1) tutor_overall_level_subject.sql

2) tutor_subject_level.sql

3) tutor_level.sql

4) tutor_subject.sql

 

In tutor_level.sql, there are 11 'level_id'

level id 1 == Pre-School

level id 2 == Lower Primary School

level id 3 == Upper Primary School

level id 4 == Lower Secondary

etc....

 

 

As you can see from the code below,

I am trying to retrieve 'lower primary' records only and place it in a table

Subsequently, 'upper primary' records and place it in another table

 

However when I executed the code, I receive a blank page. What could have gone wrong? Thanks

if ($row3['level_id'] == 2){}

if ($row3['level_id'] == 3){}

 

<?php
//Retrieve Lower Primary 
if ($row3['level_id'] == 2) {
	echo'<table><tr>'; 
                echo '<td class="label">Lower Primary</td>';
	$count = 0; 
	while($row3 = mysqli_fetch_array($data3)) {

		if ($count % 5 == 0) { 
			echo "</tr><tr>"; $count = 0;
		}
		echo '<td>' . $row3['subject_name'] . '</td>';
		$count++; 
	} 
	echo '</tr></table><br/>'; 
}

//Upper Lower Primary 
if ($row3['level_id'] == 3) {
	echo'<table><tr>'; 
                echo '<td class="label">Upper Primary</td>';
	$count = 0; 
	while($row3 = mysqli_fetch_array($data3)) {

		if ($count % 5 == 0) { 
			echo "</tr><tr>"; $count = 0;
		}
		echo '<td>' . $row3['subject_name'] . '</td>';
		$count++; 
	} 
	echo '</tr></table><br/>'; 
}
?>

 

 

//This is my complete code

<?php
$query3 = "SELECT sl.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">';

if ($row3['level_id'] == 2) {
	echo '<td class="label">Lower Primary</td>';
                echo'<table><tr>'; 
	$count = 0; 
	while($row3 = mysqli_fetch_array($data3)) {

		if ($count % 5 == 0) { 
			echo "</tr><tr>"; $count = 0;
		}
		echo '<td>' . $row3['subject_name'] . '</td>';
		$count++; 
	} 
	echo '</tr></table><br/>'; 
}

echo '</div>'; //End of panel 4
?>

 

 

 

Link to comment
Share on other sites

What could have caused it to be false?

 

var_dump doesn't return any value.

 

The problem your having is that $row3 isn't defined yet where you are trying to use it. Your basically doing....

 

if ($row3) {
  // code that defines $row3
}

 

It makes no sense.

Link to comment
Share on other sites

Hi Thorpe,

 

Thanks for your reply.

 

May I check with you, what do you mean that I have yet defined the $row3?

 

Correct me if I am wrong, in my $query3, I have queried sl.level_id AS level_id, which I believe there are already values in it, therefore when I execute '$row3['level_id'] == 2', why isn't it able to pull out data which has level_id == 2?

 

Thanks

 

$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'] . "'";

Link to comment
Share on other sites

Thanks Thorpe,

 

I got the idea already. It works now. One last question.

 

echo ''.$row3['subject_name'].', ';

As I did an echo for the subject name in the while loop and realised the last result will still have a comma, example

English, Math, Science, Chinese,

 

How do I code to prevent the last value to have a comma?

I would like to achieve

English, Math, Science, Chinese <--Without the comma at the last value. Thanks

 

 

$data3 = mysqli_query($dbc, $query3)
  or die(mysqli_error($dbc));

  if (mysqli_num_rows($data3) > 0) {


echo '<div id="panel4">';
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 '</div>'; //End of panel 4
} //End of IF for $query and $data (Teaching Subjects)

Link to comment
Share on other sites

Hi Thorpe,

 

There is another issue which I am facing

 

Currently executing the code below, it gives me a nice organized results, attachment in correct.jpg

echo '<span class="row_header">Lower Primary</span><br/>';
	while($row3 = mysqli_fetch_array($data3)) {
		if ($row3['level_id'] == 2) {
			echo ''.$row3['subject_name'].', ';
		}

	}

 

 

However after adding a duplicated code for 'Upper Primary', my results return in a warp manner, in attachment incorrect.jpg. Did I place my code wrongly? How do I achieve correct.jpg format for 'Upper Primary'? Thanks

<?php
//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'].', ';
		}
//Upper Primary
	echo '<span class="row_header">Upper Primary</span><br/>';
		if ($row3['level_id'] == 3) {
			echo ''.$row3['subject_name'].', ';
		}
	}
?>	

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi all,

 

I need some help here. Been trying to get this right

 

Currently when I executed the code below, I will have a return value of

 

Lower Primary

English, Mathematics, Science, Chinese,

 

echo '<span class="row_header">Lower Primary</span><br/>';
	while($row3 = mysqli_fetch_array($data3)) {
		[color=red]if ($row3['level_id'] == 2)[/color] {
			echo ''.$row3['subject_name'].', ';
		}
	}

 

And I have decided to add the similar code for 'Upper Primary'. After adding the Upper Primary code, my return result is still

Lower Primary

English, Mathematics, Science, Chinese,

 

I don't see 'Upper Primary' result appearing. Any advice? Thanks

 

 

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/>';

 

//Upper Primary

while($row3 = mysqli_fetch_array($data3)) {

if ($row3['level_id'] == 3) {

echo ''.$row3['subject_name'].', ';

}

}

 

Link to comment
Share on other sites

if you're going to loop over the result twice, you'll need to reset it before the second time.

 

while($row3 = mysqli_fetch_array($data3)) {
         if ($row3['level_id'] == 2) {
            echo ''.$row3['subject_name'].', ';
         }
      }

echo '<br/>';

mysql_data_seek($data3, 0); // Move internal result pointer back to first record.

//Upper Primary
      while($row3 = mysqli_fetch_array($data3)) {
         if ($row3['level_id'] == 3) {
            echo ''.$row3['subject_name'].', ';
         }
      }

Link to comment
Share on other sites

Hi BlueSkyIs,

 

Sorry that I didnt see you post. Omg! It works! Thanks!!!!

 

One last question, I have asked before, but didnt received an answer yet.

 

Currently my result displayed a 'comma' at the end

 

Lower Primary

English, Math, Science, Chinese,

 

How do I remove the comma which is bold in red? Thank you so much

 

while($row3 = mysqli_fetch_array($data3)) {
         if ($row3['level_id'] == 2) {
            echo ''.$row3['subject_name'].', ';
         }
      }

Link to comment
Share on other sites

There is another issue here.

 

Currently the user did not select any subjects in pre-school, therefore there isnt any value, however the header will still show 'Pre-School' with empty value. Attached is the picture for your reference.

 

How do code, so that only the 'level_id' which has value or selected by users will appear?

 

Do I user (!empty) command? I tried, but it gives an error. Anyone can help me out with this?

 

Error Code

//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 workable code, but still trying to not show Pre-School, cuz it doesnt have any selected value

<?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

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.