Jump to content

Recommended Posts

I'm just wondering how I would use a string as part of the name of a new string, like this...

 

$*insert string*sel = "selected";

 

Meaning that I have a string, and say the value is "blue". It's pulling the value "blue" from a database, and depending whats in the database, I need a string named $bluesel.

 

I'm acually having a few problems like this, and I cant find anything on Google that puts my in the right direction.

 

Thanks,

-Seven_Rings

Hey. I didnt think about it this way. And I think this might work. You know how when you are coding, its like a plan, and I overlooked this.

 

I really appreciate the help. While I'm here, I might ask one more thing...

 

$result = mysql_query("SELECT * FROM table")

or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

//How do I use a string in here?

echo $row[$string . 'level'];

}

 

I have tried a few differant things, none of which work.

These are two examples.

echo $row['$stringlevel'];

echo $row[$string . 'level'];

 

I am trying to pull certain results from my table. And those results need to be defined by that string.

 

If the string = seven, than I want to pull all entrys from sevenlevel.

 

Any ideas?

 

*This is acually from a post I made last night, that nobody has responded too.

 

Thanks,

-Seven_Rings

I think I understand what you're trying to do there, you can try:

$stringlevel = $string . 'level';
$result = mysql_query("SELECT * FROM table")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
//How do I use a string in here?
echo $row['$stringlevel'];
}

Then if $string is blue, it would be:

echo $row['bluelevel'];

 

If you have a column called for instance 'sevenlevel' and $string == 'seven' then

 

echo $row[$string . 'level'];

 

Should have worked. Alternatively you could do

 

echo $row["{$string}level"];

 

Or concat it beforehand like Zhadus mentioned.  Perhaps $string does not contain the value you are expecting?

Alright... I figured out that problem atleast, and you guys may be disappointed with the results.

 

The table name wasnt capitalized, and the string value was.

 

How can I get around this? How can I make the string lowercase? I am getting the value of the string from...

<select>
<option>Blue</option>
<option>Green</option>
</select>

If I changed that too...

<select>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>

Would that work?

 

Thanks,

-Seven_Rings

*Thorpe, though the problem is fixed, I still might do that to save alot of code. So thanks :P

 

Let me answer myself, It is working. And its all thanks to you guys ^_^

 

The last big thing on my plate right now is this, I'm sorry to do this to you guys, but I am still having trouble with the first thing. But this time, I will give alot more example.

 

My code is something like this...

 

<?
$color = $_POST["color"];

$submit = "INSERT INTO test (currentcolor) VALUES('$color')";
mysql_query($currentskillsubmit);

$result = mysql_query("SELECT currentcolor FROM test WHERE id = 1") 
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
$*$row[currentcolor]*sel = "selected";
}
?>

<form enctype="multipart/form-data" action="index.php" method="POST">
<select name="color">
<option <? echo $bluesel; ?>>Blue</option>
<option <? echo $greensel; ?>>Green</option>
<select>
</form>

 

When a color is changed and submitted, its stored in the database. And the next time the page is refreshed, the database is read, and the last color selected needs to be "selected" in the drop down. So if the database reads "blue", I need to set a string $bluesel = "selected";

 

The problem is, that I need to make a string, based on the value of anouther string, and I dont know how to do that.

// This would make the current color from the database selected. The problem is that this isnt corrent syntax.
$$row[currentcolor]sel = "selected";

 

I hope I'm not annoying you guys with my questions, but I really cant find answers anywhere else.

 

Thanks a lot,

-Seven_Rings

<?php
$color = $_POST["color"];

$submit = "INSERT INTO test (currentcolor) VALUES('$color')";
mysql_query($currentskillsubmit);

if ($result = mysql_query("SELECT currentcolor FROM test WHERE id = 1")) { 
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_array( $result )) {
  }
}

?>

<form enctype="multipart/form-data" action="index.php" method="POST">
<select name="color">
<option <?php echo (isset($row['currentcolor']) && $row['currentcolor'] == 'Blue') ? '"selected=select"' : ""; ?>>Blue</option>
<option <?php echo (isset($row['currentcolor']) && $row['currentcolor'] == 'Green') ? '"selected=select"' : ""; ?>>Green</option>
<select>
</form>

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.