Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Show value in cell as text

Options
  • 08-09-2014 8:53pm
    #1
    Registered Users Posts: 1,096 ✭✭✭


    Hi,

    Just wondering if someone can help me out as I am having a brain freeze at the moment. I'm fairly new into PHP & MySQL, but have hit a roadblock here with what I have no doubt has a simple solution.

    I have a table which has a number of columns, one which is county. Each county (due to a jQuery form element) is in-putted as a number, taken from another table which has a list of counties matched with a id. When displaying the output of a row, I need to show that county as the name of that county rather than the number/id. Is there a straightforward way to do that? I have been messing around with either an if statement or an array, but I'm not sure exactly what the solution is.

    Any ideas appreciated!


Comments

  • Registered Users Posts: 67 ✭✭The Gibmiester


    The county name should be returned in the SQL result set. You may need to do an SQL JOIN on the counties table then update the PHP to output the correct column in the result set. Can you post the SQL statement that returns the data and the PHP that prints it out?


  • Registered Users Posts: 1,096 ✭✭✭ImDave


    Thanks for the reply.

    So the main query to return the row which has the county as an id/number is as follows:

    [PHP]$query="SELECT * FROM `offices` WHERE (`id` = '$id') AND (`active` = 'Yes') LIMIT 0, 1 ";[/PHP]

    That will return a value of say 15 which in the counties table corresponds to Dublin. What I need to do is match the returned value of 15 from the offices table with the value of 15 in the counties table.

    Thanks!


  • Registered Users Posts: 67 ✭✭The Gibmiester


    So assuming you have a table called 'counties' and the county id column in the office table is called 'county', the following code should return a result set with a combination of the appropriate office row and county row.

    [PHP]$query="SELECT * FROM offices o, counties c WHERE (o.id = '$id') AND o.county = c.id AND (`o.active` = 'Yes') LIMIT 0, 1 ";[/PHP]

    I gave the table aliases for handiness. Also, for optimisation, you should only specify the columns you need rather than SELECT *.


  • Registered Users Posts: 1,096 ✭✭✭ImDave


    Cheers! That worked very well. Thanks for your help.


  • Registered Users Posts: 2,781 ✭✭✭amen


    Don't use Select *
    Only return the columns you need.


  • Advertisement
Advertisement