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
Make/Print array made of serialized arrays
Old 12-01-2010, 06:05 PM Make/Print array made of serialized arrays
Skilled Talker

Posts: 61
Trades: 0
We are doing a reader survey, and many of the questions involve multiple selection answers (in this case, we're using check boxes). So of course, answers are arrays, and are then stored in mysql after being serialized.

My problem is arising when trying to get the collected info out in a readable format. I would like to spit out each distinct value and then a count of how many readers checked it.

For example, with a single select question, I'm doing it this way with no problem:

PHP Code:
<? $resAge mysql_query("select age, count(*) as numAge from survey2010 group by age");
                            
while (
$rowAge mysql_fetch_array($resAge)){
    
extract($rowAge);
    
$perAge round(($numAge $respondants) *1000);
    print(
'<tr><td class="surveyForm"> ' .$age .'</td><td class="surveyForm">' .$numAge .'</td><td class="surveyForm">' .$perAge .'% </td></tr>');
        }
?>

I'm completely baffled on how to make this work with the above described situation in which each row from mysql is possibly an array itself. Suggestions would be very much appreciated. Thanks.
gregory is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-01-2010, 07:16 PM Re: Make/Print array made of serialized arrays
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
What's the definition of survey2010?
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-01-2010, 08:36 PM Re: Make/Print array made of serialized arrays
Skilled Talker

Posts: 61
Trades: 0
Quote:
Originally Posted by PaulW View Post
What's the definition of survey2010?
survey2010 is the name of the mysql table
gregory is offline
Reply With Quote
View Public Profile
 
Old 12-02-2010, 07:25 AM Re: Make/Print array made of serialized arrays
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
Quote:
So of course, answers are arrays, and are then stored in mysql after being serialized.
This is a bad answer... you should use another related mysql table, with your answers.

As you know, mysql doesn't unserialize... so you have to make all with PHP

make a select with all fields {
make unserialize and create another array like, $arr[$age][] = answers
to count and do whatever you like BUT...
}

it takes almost double time, because you could do this only from mysql if you had a good database structure
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 12-02-2010, 11:18 AM Re: Make/Print array made of serialized arrays
Novice Talker

Posts: 4
Name: Anthony Rossbach SBIH Inc. CEO.
Trades: 0
First, you don't want to use print but rater echo, echo is both faster and more stable then print.

As far as the code goes try the post above me but remember that echo is better to code with. and always use the full <?php in the start of your code to make sure it will work with all php versions and servers set up differently.
anthonyrossbach is offline
Reply With Quote
View Public Profile
 
Old 12-02-2010, 11:19 AM Re: Make/Print array made of serialized arrays
Skilled Talker

Posts: 61
Trades: 0
Interestingly enough, that's how I've done similar things in the past - with multiple tables. But at some point, I feel like someone told me it was a really clumsy, inefficient way to do things.. hence this. Perhaps I need to reconsider.
gregory is offline
Reply With Quote
View Public Profile
 
Old 12-02-2010, 11:20 AM Re: Make/Print array made of serialized arrays
Skilled Talker

Posts: 61
Trades: 0
Quote:
Originally Posted by anthonyrossbach View Post
First, you don't want to use print but rater echo, echo is both faster and more stable then print.

As far as the code goes try the post above me but remember that echo is better to code with. and always use the full <?php in the start of your code to make sure it will work with all php versions and servers set up differently.
thanks for the advice
gregory is offline
Reply With Quote
View Public Profile
 
Old 12-02-2010, 01:47 PM Re: Make/Print array made of serialized arrays
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by anthonyrossbach View Post
First, you don't want to use print but rater echo, echo is both faster and more stable then print.
The performance difference is negligible. What do you mean by echo is more stable?. If anything print is more stable considering that there is a special case where print will work and echo won't.

Quote:
Originally Posted by anthonyrossbach View Post
and always use the full <?php in the start of your code to make sure it will work with all php versions and servers set up differently.
This is absolutely true. Using short tags in place of the php tag is the best way to kill the portability of your code.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-02-2010, 01:50 PM Re: Make/Print array made of serialized arrays
Skilled Talker

Posts: 61
Trades: 0
I reworked this to use separate tables for each multiple-select question. Much easier.

thanks for all the help.
gregory is offline
Reply With Quote
View Public Profile
 
Old 12-04-2010, 10:13 AM Re: Make/Print array made of serialized arrays
Extreme Talker

Posts: 156
Trades: 0
Nice you got it working, having separate tables sometimes is the easiest solution
dagaul101 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Make/Print array made of serialized arrays
 

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.26809 seconds with 12 queries