|
There's an easier method: nl2br()[^]
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
Quiltfish wrote: It did but didn't store linebreaks properly
Huh? how would it matter what is inside your string? what happens exactly? And on what system is this?
Are you sure your newlines are \n and not \r or even \r\n ?
FWIW: Googling PHP str_replace newline brings up lots of things including this: nl2br[^]
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Luc Pattyn wrote: Huh? how would it matter what is inside your string? what happens exactly? And on what system is this?
Are you sure your newlines are \n and not \r or even \r\n ?
Yep, I'm sure newlines are just \n.
I copy-paste the appropriate texts to store from a related blog (No, not stealing material. The blog is a parent site, my site is sort of an archive) and before this, when displaying the text back from the database, it all ran together into one big paragraph. With hebrevc (or rather, nl2br once I can verify without a 500 code..) it displays neatly formatted as it should.
Thanks for the suggestions too! Apparently my google-fu is weak today..
|
|
|
|
|
So basically what you are saying is, it is not the database that is at fault, you simply do need a conversion from non-HTML text (including newlines), to text that is fit for displaying on a web page.
If so, you need a lot of conversions, such as newline to <br /> but also & to & and < to < etc.
There is bound to be a PHP function that does all that in one go.
You may look here[^], htmlspecialchars() looks interesting.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Oh wow, that would've saved me some other str_replace calls (which worked fine,btw,unlike the \n -> ).
At first, I wanted to do that conversion before storage, in a fit of premature optimization, so as not to have to do the replace's every time the text is viewed but only once on storing.
Yeah, I was young and newbie-ish when I first wrote this script (well, slightly more so anyway )
|
|
|
|
|
Here is some general advice for you:
- store data in a simple form, before processing (so when processing is wrong, data is still intact);
- have a problem? always ask yourself "Am I the first one in the world to have this problem? who else would have it? can he help me?"
- almost everything is one Google away; typing a question as a full sentence often is sufficient to find the answer right away.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hello everyone I have a big problem with this function that i wrote myself and i am sure i messed something
Could somebody look at it and help me Debug it ?
function am_getlatestscores($db, $cat) {
$sql = "SELECT * FROM `AMCMS_highscores` JOIN `AMCMS_users` WHERE `primkey`=`username` ORDER BY `primkey`='$id' DESC LIMIT 10;";
$res = am_queries($db, $sql);
return $res;
}
The function is suppose to connect to the db, select the latest record from AMCMS_highscores and join the AMCMS_highscores (userkey) field with the AMCMS_users database and field (userkey). So then i will get the latest highscores from highscores database and get the username by looking for the userkey -> username. Display latest highscore and the user who made it.
Could anyone help me make this dream come true Please
|
|
|
|
|
djkee wrote: ORDER BY `primkey`='$id'
Order by doesn't have a condition, just the field which you sort the results by. Did you mean to use AND rather than ORDER BY?
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
I am trying to order them by primkey.
Each new score has a higher primkey, so the latest will be with the highest number and thats why i was trying to order them by primkey
Hope this helps
|
|
|
|
|
Read the documentation[^]
ORDER BY `primkey` DESC
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
Thanks, i fixed it and its compiling now but i cant get the scores out
Here is the function:
function am_getlatestscores($db, $cat) {
global $db;
$sql = "SELECT * FROM `AMCMS_highscores` JOIN `AMCMS_users` WHERE `primkey`=`username` ORDER BY `primkey` DESC LIMIT 10;";
$res = am_queries($db, $sql);
return $res;
}
Here is how i output them in another file:
$higharray = am_getlatestscores($db, 'none', 'list');
foreach ($higharray as $hhscore) {
echo '<font><a href="./index.php?cat='.hhscore.'">$hhscore</a></font>
But it wont output anything I am sorry if its very simple but i am learning
|
|
|
|
|
djkee wrote:
echo '<a href="./index.php?cat='.hhscore.'">$hhscore</a>';
You're missing the $ in front of hhscore. That and hhscore is an array. The array can be accessed by either numerical or string keys.
You can use $hhscore[ 'column1' ] or $hhscore[ 0 ] depending on your preference is.
I'm not saying that they will both work as I don't know whether $hhscore is associative or not (or both).
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
I tried using $hhscore[ 0 ] but the script just crashes
What do you mean by if $hhscore is associative or not (or both) ??
With that mysql query and the echo should i be able to get the records from the database ?
This is error i get:
Warning: Invalid argument supplied for foreach() in /home/myusername/public_html/cache/%%45^45E^45E480CD%%index.tpl.php on line 37
Thank you for all the help
modified on Wednesday, November 25, 2009 6:24 AM
|
|
|
|
|
After saying what I said before, it may not even be an array, what exactly do you have in am_queries() ?
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
Here is the function am_queries
function am_queries($db, $sql, $verboseErr=3, $cacheCmd=1, $cache_dir = './cache/') {
global $cache_config;
global $cache_page_time;
global $numberOfQueries;
$exempt = false;
$checkOne = explode(' ', $sql, 2);
if (strtoupper($checkOne[0])=='INSERT' || strtoupper($checkOne[0])=='UPDATE' || strtoupper($checkOne[0])=='CREATE' || strtoupper($checkOne[0])=='DROP' || strtoupper($checkOne[0])=='DELETE') {
$exempt = true;
}
These are the errors i get when trying to output the query.
Warning: Missing argument 2 for am_getlatestscores(), called in /home/myusername/public_html/cache/%%45^45E^45E480CD%%index.tpl.php on line 36 and defined in /home/myusername/public_html/includes/gamefunctions.php on line 491
Warning: Invalid argument supplied for foreach() in /home/myusername/public_html/cache/%%45^45E^45E480CD%%index.tpl.php on line 37
Thanks for helping me fly904
modified on Wednesday, November 25, 2009 2:39 PM
|
|
|
|
|
if I may...
your first warning is that your are missing the 2nd argument in your function am_getlatestscores, which is $sql. This warning is being called from called in /home/myusername/public_html/cache/%%45^45E^45E480CD%%index.tpl.php on line 36. Open this php script and go to line 36 and look at how the $sql argument is built and passed. Also count your arguments you have 5 listed, but only the last 3 have default values. So you MUST have at least 2 arguments in the function call.
The foreach is expecting an array to be passed, and it is not present.
the 2 common usages of foreach is:
foreach($myArray as $value)
{
echo "Val: ".$value." ";
}
...or...
foreach($myArray as $key=>$value)
{
echo "Key:".$key." Val: ".$value." ";
}
given an array of...
$myArray = array(a,b,c);
The first foreach will produce...
Val: a
Val: b
Val: c
The second foreach....
Key: 0 Val: a
Key: 1 Val: b
Key: 2 Val: c
So long story short, the value you have passed to your foreach is not an array.
looking back at your code...
$higharray = am_getlatestscores($db, 'none', 'list'); foreach ($higharray as $hhscore)
...try testing the $higharray variable...
It may be undefined, an empty string or FALSE.
|
|
|
|
|
Hey everybody,
I posted something simmilar while ago, but didn't received enough info.
What I am looking for is php script that would allow users to create an album, upload multiple files, store files itself on server, and data about them in database. what else Im looking is ability to create thumbnail at the same time when file is uploaded.
I was searching quite a lot and found this one: http://articles.sitepoint.com/article/php-gallery-system-minutes/1[^]
Basically it should do everything what I am looking for, but unfortunetely I am not able to implement it.
When I download source files from this tutorial, and upload them to my server, these doesnt do all the stuff. When I run preupload.php it seems that files are uploaded, and confirmation message appears, data is stored in one of the tables in database, but no files or thumbnails appears on server....
Any ideas? Am I missing something big in here or the tutorial is inncomplete?
If anybody knows any other tutorials that would do something like this one pls give me a link
Thank you in advance.
|
|
|
|
|
I ran across this a few years ago and found I could just handle the raw post data myself.
Recently I had to write a script to handle an ajax call, from grease monkey in fact, and the raw post data is there and the post variable is set but it is blank.
I would prefer to use the post variable instead of having to manually parse the data.
A sample html/js/php fragments of what I am doing follows:
<?php
fwrite($fp,file_get_contents("php://input"));
?>
I can read the raw post data.
But when I use something like
<?php
echo $_POST['name'];
?>
it is blank
My source data is built via js like this...
js...
var postvar = "name="+encodeURI(name)+"&markup="+encodeURI(markup)
...js
Name is a identifier and markup is a select html code.
I am curious if anyone has figured out why php fails to parse the raw data sent to a page.
|
|
|
|
|
cjoki wrote:
<?php
echo $_POST['name'];
?>
Try print_r( $_REQUEST ); to see what was sent. Without seeing the rest of your javascript I can only guess at what the problem is.
The most common problems are that the http request was opened with 'GET' rather than 'POST' eg.
http_request.open('POST',<br />
url, true);
Or the parameters weren't sent eg. http_request.send(postvar);
Or I may have missed the point, so please clarify.
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
fly904,
thanks for the response but I found my error. Using the GM_xmlhttpRequest object I mistakely set the content type to...
GM_xmlhttpRequest({
method: 'POST',
url: '[URL deleted for post]/copy_prdt.php',
data: postdata,
headers:
{
'Content-type': 'x-www-form-urlencoded'
},
onload: function(responseDetails)
{
alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
},
onError: function(responseDetails)
{
alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
}
});
and it needed to be...
GM_xmlhttpRequest({
method: 'POST',
url: '[URL deleted for post]/copy_prdt.php',
data: postdata,
headers:
{
'Content-type': 'application/x-www-form-urlencoded'
},
onload: function(responseDetails)
{
alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
},
onError: function(responseDetails)
{
alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
}
});
It is odd, I did not get any error messages and the script seem to work fine, except for the post variable not having the data parsed into it.
I can only guess that the php parser was confused and quietly aborted the parsing of the post data.
|
|
|
|
|
cjoki wrote: I can only guess that the php parser was confused and quietly aborted the parsing of the post data.
Debugging Ajax is a bitch, as it doesn't always throw an error, because technically it works although not the way you want it .
If at first you don't succeed, you're not Chuck Norris.
|
|
|
|
|
I tend to use an "output" box for this that just says what the code is doing step by step on js side and Firefox Error Console. But some errors do still seem to hide.
A function you may know: To read headers sent using PHP you can use:
<?php
var_dump(headers_list());
?>
|
|
|
|
|
var_dump prints the results to a browser. In an ajax based system this can be messy.
Although I could have used var_export, I instead used fwrite($fp,file_get_contents("php://input")); so to write the contents of the raw post data before the parser works on filling the $_POST array. Doing it this way allows the server side script of a ajax solution to continue and return some value so your javascript can shutdown gracefully while allowing you to capture the content.
I also use a firefox addon called httpfox so I can observe the communication between my browser and the server.
|
|
|
|
|
httpfox
Been looking for something like that for a while. Plain & Simple.
|
|
|
|
|
its good to share
|
|
|
|