Jump to content

How to change one of the results to a string


SamNewbie

Recommended Posts

Hey,

 

I have got the results displayed from the database when it gets the ID in the URL but I am having a problem with one the results, in lists.php, there is a list of cities as an array

 

city=array(0=>"London")

 

But in the results, it is displaying the '1' instead of 'London', i need it to display 'London'. Below is my coding... it is all connected to the database. How do i change the data type?

 

<?php
 
if (isset($_GET['id'])){
$query="SELECT*FROM user WHERE user_id=".$_GET['id'];
$results = mysql_query($query) or die (mysql_error());
 
while($row = mysql_fetch_array($results)){
 
echo "Name: ".$row['user_firstname']." ".$row['user_surname'];
echo "<br /><br />";
echo "Date of Birth: ".$row['user_dob'];
echo "<br /><br />";
echo "Gender: ".$row['user_gender'];
echo "<br /><br />";
echo "City: ",$row['user_city'];
echo "<br /><br />";
 
}
}
 
Thanks for any help!
 
 

 

 

Link to comment
Share on other sites

Your mean $row['user_city'] returns 1 and not London?

 

The user_city field in the users table may be used as a foreign key to retrieve the actual city name from another table in your database?

 

Hi Ch0cu3r,

 

Yes, user_city and gender are listed as numbers.

For this project, I cannot change the data within the database and the two entities have to be kept as numerical format.

 

Is there anyway to make gender (currently 1 is male and 2 is female), display as male and female respectively? 

Is there a way to do this with city names as well? Currently 1 = Aberdeen, 2 = London etc...

 

Thanks for your help.

Link to comment
Share on other sites

Yes, you'll need to use JOINS in your query to fetch the data from the other tables, example

 

Can you post a the schema for the gender and city tables?

 

 

 

For this project, I cannot change the data within the database and the two entities have to be kept as numerical format.

That is good practice, it called data normalisation.

Link to comment
Share on other sites

You never said that before, so the cities are listed in a $citenames array and the same applies for gender?

 

In that case you need to first include lists.php

include 'lists.php'; 

and the use  $row['user_city'] as the array index for the $citynames array, eg

echo "City: ", $citynames[ $row['user_city'] ];

If the genders is also listed in lists.php then you'll do a similar thing for gender

echo "Gender: ". $gender_array_var[ $row['user_gender'] ];

Change $gender_array to the name of the variable that holds the array of genders

Edited by Ch0cu3r
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.