Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Poeben

macrumors 6502
Original poster
Jul 29, 2004
346
0
So here is my problem, everything works fine when hard-coded, i.e:

Code:
//perfrom query to database and assign the variables
$query = "SELECT * FROM content WHERE section='home';" or die(mysql_error());

What I want to do is replace 'home' with a variable that is obtained by $_GET from the URL. I am getting the variable ok, as I can echo it using var_dump.

Code:
<?php

// $_GET for admin section
$admin = $_GET['admin_id'];

//snip database connect

//perfrom query to database and assign the variables
$query = "SELECT * FROM content WHERE section='"echo $admin"';" or die(mysql_error());

$result=mysql_query($query);

//snip loop code

//Test that ?admin_id variable is working correctly
var_dump($_GET);
echo $admin;
?>


I have tried all sorts of single/double quotes, escape backslashes etc.. I have only been learning php/MySQL for about 2 days so I am a bit of a noob.

Thanks for any help
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
Poeben said:
So here is my problem, everything works fine when hard-coded, i.e:

Code:
//perfrom query to database and assign the variables
$query = "SELECT * FROM content WHERE section='home';" or die(mysql_error());

What I want to do is replace 'home' with a variable that is obtained by $_GET from the URL. I am getting the variable ok, as I can echo it using var_dump.

remove the echo.

Code:
$query = "SELECT * FROM content WHERE section='$admin';" or die(mysql_error());
 

Poeben

macrumors 6502
Original poster
Jul 29, 2004
346
0
Cool, thanks. Funny, I originally was not using the 'echo'; I think I was getting the quotes wrong among other things. Then I saw an example where they were using 'echo' and it just stuck in there. :p

Works great now, thanks! Now for the rest of the site...
 

Poeben

macrumors 6502
Original poster
Jul 29, 2004
346
0
Another question--textarea formatting

So, now I have gotten the code working the way I want, more or less. However, I have a textarea that I am using to update the database. How can I keep the formatting (carriage returns, extra spaces) that are input into the textarea. I am using PHP along with the $_POST method to process the form output.

thanks for any help.
 

MontyZ

macrumors 6502a
Jan 7, 2005
887
0
Poeben said:
So, now I have gotten the code working the way I want, more or less. However, I have a textarea that I am using to update the database. How can I keep the formatting (carriage returns, extra spaces) that are input into the textarea. I am using PHP along with the $_POST method to process the form output.
You don't have to do anything actually, just store the textarea stuff directly into the database, the newlines and extra spaces will be maintained.

However, newlines and extra spaces won't be displayed if you echo the contents to an HTML page, because newlines are not translated by the browser. So, what you'll need to do is echo it this way to turn newlines into HTML linebreaks:

echo nl2br($text);

To maintain the extra spaces is a bit more tricky and will require the use of regular expressions and the eregi() functions. You'll need to turn the additional spaces into   which is a non-breaking space (as an HTML entity).

Or, the easiest way would be to simply wrap the contents within <pre> </pre> HTML tags which will put everything in an ugly monospace font, but, will maintain the formatting even on an HTML page.
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
MontyZ said:
...
Or, the easiest way would be to simply wrap the contents within <pre> </pre> HTML tags which will put everything in an ugly monospace font, but, will maintain the formatting even on an HTML page.

You could fix the display with CSS.
 

Poeben

macrumors 6502
Original poster
Jul 29, 2004
346
0
Thanks for the tip. The 'echo nl2br($text);' did exactly what I wanted. So far 2 for 2. Good show.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.