Jump to content

Getting value from database to show it in HTML code using while loop


Recommended Posts

Hi all,

 

The below example is a workable code, taken from tutor_profile.sql table

<?php
    $query = "SELECT tutor_id, religion_id FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
    
$data = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));

// The user row was found so display the user data
if (mysqli_num_rows($data) == 1) {
	$row = mysqli_fetch_array($data);

print_r($row);
    if ($row != NULL) {
  $religion_id = $row['religion_id'];
  $tutor_id = $row['tutor_id'];

    }
    else {
      echo '<p class="error">There was a problem accessing your profile.</p>';
    }
  }

<!--Religion-->	  
  <tr>
      <td class="label">Religion:</td>
	<td>
	<select id="religion_id" name="religion_id">
		<option value="1" <?php if (!empty($religion_id) && $religion_id == '1') echo 'selected = "selected"'; ?>>Buddhism</option>
		<option value="2" <?php if (!empty($religion_id) && $religion_id == '2') echo 'selected = "selected"'; ?>>Christianity</option>
		<option value="3" <?php if (!empty($religion_id) && $religion_id == '3') echo 'selected = "selected"'; ?>>Hinduism</option>
		<option value="4" <?php if (!empty($religion_id) && $religion_id == '4') echo 'selected = "selected"'; ?>>Islam</option>
		<option value="5" <?php if (!empty($religion_id) && $religion_id == '5') echo 'selected = "selected"'; ?>>Taoism</option>
		<option value="6" <?php if (!empty($religion_id) && $religion_id == '6') echo 'selected = "selected"'; ?>>Others</option>
	</select>
	</td>
  </tr>
?>

 

As you can see I have hard coded the names of the religion in html code

example - <option value="3" <?php if (!empty($religion_id) && $religion_id == '3') echoselected = "selected"'; ?>>Hinduism</option>

 

And if our record shows that the tutor has previously selected '3', it will reflect as 'hinduism' in his profile. View profile.jpg for example

example - <option value="3" <?php if (!empty($religion_id) && $religion_id == '3') echo 'selected = "selected"'; ?>>Hinduism</option>

 

In fact, these names can be found in another table called religion.sql, but I hard coded it anyway, without using loop (while function), since there are only 8 names

<!--Religion-->	  
  <tr>
      <td class="label">Religion:</td>
	<td>
	<select id="religion_id" name="religion_id">
		<option value="1" <?php if (!empty($religion_id) && $religion_id == '1') echo 'selected = "selected"'; ?>>Buddhism</option>
		<option value="2" <?php if (!empty($religion_id) && $religion_id == '2') echo 'selected = "selected"'; ?>>Christianity</option>
		<option value="3" <?php if (!empty($religion_id) && $religion_id == '3') echo 'selected = "selected"'; ?>>Hinduism</option>
		<option value="4" <?php if (!empty($religion_id) && $religion_id == '4') echo 'selected = "selected"'; ?>>Islam</option>
		<option value="5" <?php if (!empty($religion_id) && $religion_id == '5') echo 'selected = "selected"'; ?>>Taoism</option>
		<option value="6" <?php if (!empty($religion_id) && $religion_id == '6') echo 'selected = "selected"'; ?>>Others</option>
	</select>
	</td>
  </tr>

 

Currently I am facing an issue, I guess I will need to use looping, as there are 22 names in another table which I will need to call forth, tutor_educational_level.sql, and the number of names get more and more in other tables.

 

My question is, how do I pull out the entire list of names into a drop down box and yet showing the selected name which the user has chosen, more elaboration can be seen in profile.jpg. In profile.jpg - as you can see the list of names are shown in the drop down box and the system is able to decipher the chosen name.

 

Another

Example

1) N level

2) O level

3) A level

4) University

 

User selected '3', which is A level, and system would still show the list of educational_names in a drop down box,, but selecting A level as the one to appear.

 

Example

1) N level

2) O level

3) A level (selected)

4) University

 

It should have the same overall result as the religion which I have stated above, however this time round, it is using looping function (while) to retrieve the entire list of names, select and show the name which the user has chosen

 

 

Below is my code, and I know it is wrong, but generally would like to relate my idea across.

<?php
<!--Teaching Credentials-->	  
  <tr>
      <td class="label">Teaching Credentials:</td>
	<td>
	<?php
		echo '<select name="educational_level" id="educational_level">';

		$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
		or die(mysqli_error($dbc));
		$query = "SELECT tp.educational_id, el.educational_name AS educational_name, el.educational_id AS list_educational_id " . 
				  "FROM tutor_profile AS tp " .
				  "INNER JOIN * tutor_educational_level AS el USING (educational_id) " .
				  "WHERE tp.tutor_id = '" . $_GET['tutor_id'] . "'";
		$sql = mysqli_query($dbc, $query)
		or die(mysqli_error());



			while($data = mysqli_fetch_array($sql)) {
				echo'<option value="'.$data['list_educational_id'].'">'.$data['educational_name'].'</option>';
					if (!empty ($data['educational_id']) && ($data['educational_id']) == ($data['list_educational_id'])) {
						echo 'selected = "selected"';
					}
			}
		echo '</select><br/>';

		mysqli_close($dbc);
		?>
	</td>
  </tr>
?>

 

 

 

 

[attachment deleted by admin]

It took me a while to actually figure out what it is you're trying to solve. Actually I'm still not sure if I get it right so let me ask you.

 

Is this the problem you're having?

 

You have 2 tables:

Tutor_profile

- tutor_id

- religion_id (foreign_key)

- tutor_name

- etc

 

tutor_educational_level

- educational_id

- educational_name

 

Do you want the educational_name to be selected in a dropdown menu when you show a tutor profile?

Hi Dj Kat,

 

Thanks for you reply. Apologies for not explaining well enough.

 

Okay, perhaps let me start off with the work flow first.

 

1) User will have to register themselves in a registration form, view registrationform.jpg for an example

//As you can see, in the registration form, there is the 'educational_level', with a list of choices for user to choose, starting from 'N level student' to 'PhD Holder'.

 

//I did not hardcode these values (N level student to PhD Holder). What I have done was using while loop to get the values (N level student to PhD Holder) from tutor_educational_level.sql table.

<!--Educational Level-->
	<div>
		<label for="educational_level" class="label">Educational Level</label>
		<?php
		echo '<select name="educational_level" id="educational_level">
		<option value="">--Please select one--</option>';

		$dbc = mysqli_connect('111', '11', '11', '11')
		or die(mysqli_error());
		$query = ("SELECT * FROM tutor_educational_level ORDER BY educational_id ASC");
		$sql = mysqli_query($dbc, $query)
		or die(mysqli_error());

			while($data = mysqli_fetch_array($sql)) {
				echo'<option value="'.$data['educational_id'].'">'.$data['educational_name'].'</option>';
			}
		echo '</select><br/>';

		mysqli_close($dbc);
		?>
	</div>

 

 

//Now, take for example 'James' has completed the entire registration process, and has selected 'N level Student'. I, as the administrator log on to view their profiles, and decided to edit 'James' profile.

 

My question is...

1) How do I retrieve 'James' selected value (N level student) (example in editprofile.jpg)

2) And at the same time, show the entire list of values (N level student to PhD holder)  (example in editprofile_list_of_values.jpg)

 

//This is what I have coded, personally I know it is wrong, but I hope to achieve...

1) If 'James' selected 'N level Student', which has the value '1' in educational_level_id in tutor_profile.sql.

2) From the educational_level_id, it will look up for the 'name' in tutor_educational_level.sql

, which has the value '1'.
3) And, and it will show the selected value (1 - N level Student), in the drop down box  [b](example in editprofile_list_of_values.jpg)[/b]
4) At the same time, able to show all the entire list of choices (N level student to PhD Holder).
<!--Teaching Credentials-->	  
  <tr>
      <td class="label">Teaching Credentials:</td>
	<td>
	<?php
		echo '<select name="educational_level" id="educational_level">';

		$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
		or die(mysqli_error($dbc));
		$query = "SELECT tp.educational_id, el.educational_name AS educational_name, el.educational_id AS list_educational_id " . 
				  "FROM tutor_profile AS tp " .
				  "INNER JOIN tutor_educational_level AS el USING (educational_id) " .
				  "WHERE tp.tutor_id = '" . $_GET['tutor_id'] . "'";
		$sql = mysqli_query($dbc, $query)
		or die(mysqli_error());



			while($data = mysqli_fetch_array($sql)) {
				echo'<option value="'.$data['list_educational_id'].'">'.$data['educational_name'].'</option>';
					if (!empty ($data['educational_id']) && ($data['educational_id']) == ($data['list_educational_id'])) {
						echo 'selected = "selected"';
					}
			}
		echo '</select><br/>';

		mysqli_close($dbc);
		?>
	</td>
  </tr>

 

 

[attachment deleted by admin]

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.