Current location: Hot Scripts Forums » Programming Languages » PHP » strings in a mysql row


strings in a mysql row

Reply
  #1 (permalink)  
Old 08-16-04, 10:58 PM
Aesis's Avatar
Aesis Aesis is offline
Newbie Coder
 
Join Date: Aug 2004
Location: Kimberley B.C.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Question strings in a mysql row

Hi,
I want to be able to input strings into a mysql row but one at a time using a function. my question is how or what querys do I have to use to be able to input one string a time into the row without them all being stuck together so then I would like to be able to retrieve them and ordered from latest to oldest.

my goal is to be able to use a function to put one string at a time into the row for when the user clicks on something, then I have a text box which will retrieve the strings and display them.

I can do it so i can input a string into the row using a function, and have the textarea print it out but what I don't know how to do is instead of the textarea printing out one string, is to have it hold up to like 10 strings, and the function when it goes to input a string it jus adds it onto the current strings, then when it gets retrieved it is sorted from latest to oldest.

Any light on how to do is would much be apreciated.
__________________
Aesis - w00t
http://img36.exs.cx/img36/4426/kawaii1.gif
Reply With Quote
  #2 (permalink)  
Old 08-17-04, 02:16 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Could you make this a bit more clearer? At least I probably didn't get it right.
Reply With Quote
  #3 (permalink)  
Old 08-17-04, 04:36 AM
Aesis's Avatar
Aesis Aesis is offline
Newbie Coder
 
Join Date: Aug 2004
Location: Kimberley B.C.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
lol sorry my bad,
pretty much what im trying to do is have a function which puts strings into a mysql row, but if theres already a string in there, i want it to just make a space or something and add on to it.

So when i goto retreive the strings to display, I can retrieve the whole lot of them from one mysql row, but I dont know how to retrieve them seperatly instead of jus a whole bunch of strings stuck together in the row.
So when I display the strings you can easily tell the strings apart from each other.

Also having the ability to order them from latest to oldest by date when retrieving would be nice to ^^

hope thats clearer.
__________________
Aesis - w00t
http://img36.exs.cx/img36/4426/kawaii1.gif
Reply With Quote
  #4 (permalink)  
Old 08-17-04, 06:23 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, I'll try something - hopefully we are talking about the same subject

Suppose you have a field STRINGS in your row as well as a numeric field ROWID which separates different rows. If the field STRINGS already has something in it, say for example, string "foo" and you would like to add -- or concatenate which is the word to use here -- it with string "bar", you could do:

UPDATE tablename SET STRINGS=CONCAT(STRINGS, "bar") WHERE ROWID=rowid

when the string "bar" would be added to the end of the content of field STRINGS and rowid specifies which row to update.

PHP function explode() is very useful if you want to chop a string into smaller strings that are separated with a special character. explode() returns an array that contains all those strings it could separate from the original.

So if we have read STRINGS from the database and we know that individual strings are separated with, for example, character #, we could do:

$strings = explode("#", $string)

assuming the original string is in variable $string.

Does this help at all?

If you have all the strings concatenated in previous way, the oldest string should be in $strings[0] and the newer will follow. array_reverse() should turn that upside down, if you want to handle newest strings first.
Reply With Quote
  #5 (permalink)  
Old 08-17-04, 09:26 PM
Aesis's Avatar
Aesis Aesis is offline
Newbie Coder
 
Join Date: Aug 2004
Location: Kimberley B.C.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
what am i doing wrong??

yeah dark that was what i was talkin bout,
I tried to do that but its jus not workin for some reason.

btw All the pages are included('page_name.php') on my login_success.php page

heres the form page: crimes.php
PHP Code:

<html>
<
head>
<
title></title>
</
head>
<
body>
<
font face="verdana" size="2" color=FFFFFF>
<
form name="crimes_form" action="login_success.php?content/process_crimes" method="post">
    <
input type="radio" name="crime" value="#You just looted a homless guy and ran" />Loot a Homeless and run<br>
    <
input type="radio" name="crime" value="#You just snagged yourself some doritos" />Shoplift the SevenEleven<br>
    <
input type="radio" name="crime" value="#You just held up a hotdog stand" />Hold up a hotdog stand<br>
    <
input type="radio" name="crime" value="#you just Mugged that poor shmuck" />Mug a stranger<br>
    <
input type="submit" value="Do The Crime" />
</
form>
</
body>
</
html
Heres my process_crimes.php page
PHP Code:

<?php
$_POST
['crime'] = $crime;
$action = new crimes;
$action->do_crime($crime);
include(
'crime_done.php');
 
?>
</body>
</html>
and heres my classes.php page which is "included" on the main login_success.php page so everybody has access to it ^^
PHP Code:

<?php
//php classes to be loaded into login_success.php
class crimes{
function 
do_crime($passed_var){
     
$_SESSION['username'] = $user;
     
$sql_query "UPDATE users SET console=CONCAT(console, '$passed_var') WHERE username=$user";
     
$sqlresult mysql_query($sql_query);
}
}
?>
and my last page which is my console.php which is the page that is in a Iframe on the login_success.php which i want to read the strongs from the row.

PHP Code:

<?php
session_start
();
$_SESSION['username'] = $userid;
if(!
$_SESSION['username'] || !$userid){
print
"**** their empty";
}
$query "SELECT console FROM users WHERE username='$userid'";
$string mysql_query($query);
$string explode("#"$string);
print 
"$string[0]<br>";
print 
"$string[1]<br>";
print 
"$string[2]<br>";
print 
"$string[3]<br>";
print 
"$string[4]<br>";
        
?>
It doesnt seem to be working, but ive been puttin little
PHP Code:

if(!$_SESSION['username'] || !$userid){
print
"**** their empty"
over the place to see what was going on and the statements were true,

so my problem is either my SESSION variables are empty so the querys wont work or
i got bad code and the above statement


Also i know the $_SESSION['username'] is workin and displays my username because on the login_success.php page I am using it there, but it jus doesnt seem to work with the above code. I only put a "session_start();" on the console page because thats the page in a IFRAME and everything else would be on "login_success.php" which has a session_start(); at the very top.
__________________
Aesis - w00t
http://img36.exs.cx/img36/4426/kawaii1.gif

Last edited by Aesis; 08-17-04 at 09:30 PM.
Reply With Quote
  #6 (permalink)  
Old 08-18-04, 12:45 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
I don't have the time to read your whole post very carefully now, but here's some things that poked my eye:

1) If you are a beginner and have problems, use GET instead of POST in your forms - this way you will see, in your browser addressbar, what values your forms are sending to your form-handlers. You can change the GETs later, if you want to make your addressbar look more tidy.

2) You probably shouldn't use long strings in radiobuttons' value -fields. This is the value that the form sends in the variable $_POST['crime'] or $_GET['crime'] and which is accessible in your form handling code. Instead, use short values that are easier to handle, like value="crime1" or value="shoplift" etc. and translate the values to text in your code.

Here's your code:
Quote:
Originally Posted by Aesis
PHP Code:

<input type="radio" name="crime" value="#You just looted a homless guy and ran" />Loot a Homeless and run<br
Here's my suggestion:
PHP Code:

<input type="radio" name="crime" value="loot" />Loot a homless guy and run<br
3) In your next code -- if I'm reading it right -- you should probably set the $crime to get value from $_POST['crime'] and not the opposite? And the same thing with the session code: NOT $_SESSION['username'] = $userid; BUT INSTEAD $userid = $_SESSION['username']; The way you have done it will actually only clear the session variables, if you haven't anything in $userid or $crime before doing it. Hopefully I have understood your meaning...

Quote:
Originally Posted by Aesis
Heres my process_crimes.php page
PHP Code:

<?php

$_POST
['crime'] = $crime;
$action = new crimes;
$action->do_crime($crime);
include(
'crime_done.php');
?>
login_success.php which i want to read the strongs from the row.
PHP Code:

<?php

session_start
();
$_SESSION['username'] = $userid;
if(!
$_SESSION['username'] || !$userid){
...

Last edited by darkfreak; 08-18-04 at 01:00 AM.
Reply With Quote
  #7 (permalink)  
Old 08-18-04, 12:59 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Oh, and by the way -- if you want to see what's going on in different variable$, print_r($_SESSION), print_r($_POST) and print_r($_GET) can help you.
Reply With Quote
  #8 (permalink)  
Old 08-18-04, 01:56 AM
Aesis's Avatar
Aesis Aesis is offline
Newbie Coder
 
Join Date: Aug 2004
Location: Kimberley B.C.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ah yes I switched tthe variables around ( $userid=$_SESSION['username']) and all smooth sailing, works like a charm

thanks for the help, much appreciated.
__________________
Aesis - w00t
http://img36.exs.cx/img36/4426/kawaii1.gif
Reply With Quote
  #9 (permalink)  
Old 08-18-04, 02:33 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
No problem, I'm glad that I could help
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Delete row from mysql error. bigkid PHP 1 08-05-04 09:36 PM
PHP, MYSQL and Strings Larrywcc PHP 2 03-27-04 08:13 AM
mysql crash IMPORTANT comby PHP 0 03-02-04 10:27 AM
great product for dumping/recovering MySQL databases Dave Brown General Advertisements 1 10-03-03 07:40 AM


All times are GMT -5. The time now is 04:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.