Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-28-2005, 01:38 PM Help with data
Average Talker

Posts: 23
Trades: 0
I am getting my data out of the database correct. What I am pulling is state abbr that a users has selected so I may get one or more for each. What I'd like to do is take that data then convert it into something like this: MD, CA, AL, FL. I have tried and tried but i either get the first state select or the last only. Any suggestions?

PHP Code:
$query "select state.abbrv from person left join person_state_rep on person.id=person_state_rep.person_id left join state on state.id=person_state_rep.state_id where person.id=".$_POST["id"]." order by state.abbrv";
    if (
mysql_connect($defaults["db_server"], $defaults["db_username"], $defaults["db_password"])) {
        if (
mysql_select_db($defaults["db_database"])) {
            if (
$result mysql_query($query)) {
                if (
mysql_num_rows($result) > 0) {
                    while (
$sqldatarow mysql_fetch_assoc($result)) {
                        
$state_rep $sqldatarow["abbrv"];
                    }

                }
                
mysql_free_result($result);
            }
            else {
                die (
"<h3>SQL Error #".mysql_errno()."</h3>\n<h4>".mysql_error()."</h4>\n<h4>Line ".__LINE__." in ".__FILE__."</h4>\n");
            }
        }
        else {
            die (
"<h3>SQL Error #".mysql_errno()."</h3>\n<h4>".mysql_error()."</h4>\n<h4>Line ".__LINE__." in ".__FILE__."</h4>\n");
        }
    }
    else {
        die (
"<h3>SQL Error #".mysql_errno()."</h3>\n<h4>".mysql_error()."</h4>\n<h4>Line ".__LINE__." in ".__FILE__."</h4>\n");
    } 
tobeyt23 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2005, 08:46 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Use a switch:

{psudocode}

PHP Code:

function GetLong($state
{
  
$state = (!get_magic_quotes_gpc()) ? addslashes($state) : $state;

  switch (
$state) {
    case 
"CA":
      
$state 'California';
      break;    
    case 
"FL":
      
$state 'Florida';
      break;

// etc

  
}
  return 
$state;

Use the function in the following fashion, for e.g. in a loop..

PHP Code:

$thisState 
$sqldatarow['abbrv'];

$state GetLong($thisState);

echo 
$state
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.

Last edited by metho; 06-29-2005 at 08:55 PM..
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 06-30-2005, 04:12 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
That just converts names - I think he wants a list of abbreviations:

tobeyt23, try this as a replacement for your inner loop:

PHP Code:
                   if (mysql_num_rows($result) > 0) {
   
$statelist = array();
   while (
$sqldatarow mysql_fetch_assoc($result)) {
      
$statelist[] = $sqldatarow["abbrv"];
   }
   
$state_rep implode(", ",$statelist);
  } 
That ought to add each of the abbreviations to an array called statelist. Then at the end the implode function will convert the array to a string, with each abbreviation separated by a comma and a space.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 06-30-2005 at 04:15 AM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Help with data
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.64029 seconds with 12 queries