5 Replies - 425 Views - Last Post: 22 September 2012 - 11:45 AM Rate Topic: -----

#1 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 23-March 09

PHP Mysql Query Question

Posted 22 September 2012 - 10:42 AM

Hey again DiC,

I am having a hard time right now, I'm totally braindead with this problem and I literally can't figure out what to do.


So what is my problem?
Well I'm working on a simple php website that will be multilingual, my idea is to save values of text (For example, the title of the page is diferent in english, german, spanish..) in mysql database and then later using mysqlquery just echo out the values for the language the user has selected.
I'll try and explain a bit better, I have a table with the fields "Name, Language, Value" (all are text fields) and a couple of rows, 2 of which are "Introtext, EN, Welcome to ..." and "Introtext, DE, Wilkomm...". I still believe this isn't the proper way of saving this type of data but it will do for now.

My problem is trying to print the values in the page, my minds a blank, it's something so easy I believe but I can't do it lol. I just need to efficiently grab all values depending on the language the visitor has chosen, and then just simply output it in my page.
All I've come up with is
$intro = mysql_query("SELECT value FROM text WHERE (lan='$lan') AND (name='Introtext')") or die(mysql_error());
echo mysql_result($intro, 0);

Of course, this works, but I don't want to create 50 queries for 50 diferent values, I really need help with a way to do this easier, if anyone can mention any possible ways to make the query-print procedure easier, I'd really appreciate it!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Mysql Query Question

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: PHP Mysql Query Question

Posted 22 September 2012 - 10:52 AM

Use a foreach loop?
Was This Post Helpful? 0
  • +
  • -

#3 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 23-March 09

Re: PHP Mysql Query Question

Posted 22 September 2012 - 10:55 AM

View PostnK0de, on 22 September 2012 - 11:52 AM, said:

Use a foreach loop?

I was actually trying to do this but I didn't know how to save multiple strings with values, you see I save the value gotten from the query, then output it within the html code.
Thanks for the fast response!
Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: PHP Mysql Query Question

Posted 22 September 2012 - 11:03 AM

I haven't done this but you can give it a try. Declare a string array, within the foreach loop, assign the value outputted by the query to the array and then display it.
Was This Post Helpful? 0
  • +
  • -

#5 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 23-March 09

Re: PHP Mysql Query Question

Posted 22 September 2012 - 11:07 AM

View PostnK0de, on 22 September 2012 - 12:03 PM, said:

I haven't done this but you can give it a try. Declare a string array, within the foreach loop, assign the value outputted by the query to the array and then display it.


Well this is what I managed to come up with earlier this afternoon after reading about arrays, but I've never worked with multidimensional arrays before so I don't know how I'd output the values in the right areas of the html code.

$result = mysql_query("SELECT * FROM text WHERE lan='$lan'") or die(mysql_error());
$valuesarray = array();
while($row = mysql_fetch_assoc($result)){
    if ( !isset($valuesarray[$row['name']] )) {
        $valuesarray[$row['name']] = array();
        }
    $valuesarray[$row['name']][$row['value']]=$row;
    }
foreach ($valuesarray as $i => $value) {
    print_r($valuesarray[$i]);
}

This post has been edited by Joshdw: 22 September 2012 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 23-March 09

Re: PHP Mysql Query Question

Posted 22 September 2012 - 11:45 AM

Okey I managed to come up with a solution, don't know if its the best answer but this is what I did to solve my issue:
function getval($data, $lan) {
	$getresult = mysql_query("SELECT value FROM text WHERE (lan='$lan') AND (name='$data')") or die(mysql_error());
	$data = mysql_real_escape_string(mysql_result($getresult, 0));
	return $data;
}

//To print a value:
echo getval('intro','EN');


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1