Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting labels from array

Status
Not open for further replies.

plarsen

Programmer
Jul 30, 2002
27
DK
Hi

I'm programming a language option, that displays labels in the correct language, but i have some trouble inserting the array values into the page.

I have the following:

<?
// Setting up a query with all labelids and the labeltext
if ($lang) {
$lab = mysql_query("SELECT labelid, english FROM labels");
}
else {
$lab = mysql_query("SELECT labelid, danish FROM labels");
}
?>

<?
// Here i want to insert the labeltext with labelid 9
?>

<?
// Here i want to insert the labeltext with labelid 3
?>

What function should i use to insert the labeltext?

/Peter
 
the mysql_query function returns a resource handle. once you have the resource you need to fetch the data from it. if the result set is not too huge i would run it into a multi-dimensional array that you can reuse through your script. if the result set is large then i'd run separate queries at each point, returning just the row you want.

ex 1
Code:
if ($lang) {
$lab = mysql_query("SELECT labelid, english as lang FROM labels");
}
else {
$lab = mysql_query("SELECT labelid, danish as lang FROM labels");
}

while ($row=mysql_fetch_assoc($lab))
{
  $tmparray[$row['labelid']] = $row['lang'];
}
<?
// Here i want to insert the labeltext with labelid 9
echo $tmparray['9'];
?>

<?
// Here i want to insert the labeltext with labelid 3
echo $tmparray['3'];
?>

ex 2
Code:
if ($lang) {
$language = "english";
}
else {
$language = "danish";
}

<?
// Here i want to insert the labeltext with labelid 9
echo getlabel('9');
?>

<?
// Here i want to insert the labeltext with labelid 3
echo getlabel('3');


function getlabel($labelid)
{
    global $language;
$sql = "select $language as lang from labels where labelid='$labelid'";

    $result = mysql_query($sql) or die ("unable to process query. " .mysql_error());

    $row = mysql_fetch_assoc($result);
    return $row['lang'];
}
 
no problems. i assume (just in case) that it is obvious that I omitted the mysql connection code in each example?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top