Current location: Hot Scripts Forums » Programming Languages » PHP » Getting all values as one string


Getting all values as one string

Reply
  #1 (permalink)  
Old 05-06-05, 02:52 PM
Programme Programme is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Getting all values as one string

I have been at this for a while, so i'll just explain: The sql query will result in one or more rows. i want to get a value from all the row and add them to a string, seperated by a comma before i add this to the screen.

The sql bit:
Code:
<?php

$select = "SELECT * FROM table WHERE id = '$uid' AND type = 'type'";
$data = mysql_db_query($sql['data'], $select) or die("Select Failed!");
  	
while ($row = mysql_fetch_array($data))
   {
      $art = $row['article_id'];		
   } 
  
$tpl->assign("article", "$art,");

?>
So if the query found 3 rows with the article_id values; art1, art2 and art3 the template assign would then be; and the result when i print this to the screen would be:

art1, art2, art3

How is this done in a simple way? Thanks!
Reply With Quote
  #2 (permalink)  
Old 05-06-05, 03:10 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
PHP Code:

while ($row mysql_fetch_array($data))

   {
      
$arts[] = $row['article_id'];        
   }

$art implode(', '$arts);

$tpl->assign("article"$art); 


I assigned each article_id to the array $arts[] and then used implode() function to put all ids in a string seperated by ", "
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 05-06-05, 03:45 PM
Programme Programme is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
I actually found a way to do this, but your way was alot easier, so that is what i'll use. Thanks. I do however have another problem. Here is the code i got to after a while. The values i get are actually ids refering to a rel name in another table, i need these names. So insted of the values; 1, 2, 3 i get name, name, name. You see?

PHP Code:

<?php


while ($row mysql_fetch_array($data))
   {
      
$arts $row['article_id'];        
      
      
$select "SELECT * FROM realname WHERE uid = '$arts'";
      
$data mysql_db_query($sql['data'], $select) or die("Select Failed!");
      
$row mysql_fetch_array($data);
          
      
$writers[] = $row['name'];
   }

$art implode(', '$writers);

$tpl->assign("comic_writer"$art); 

?>
Problem with this is that i only get the first name, not the others.
Reply With Quote
  #4 (permalink)  
Old 05-06-05, 04:18 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
maybe this problem is happing is because you are using the same variables names from outside the loop ($data, $row)
try changing them and use mysql_query() instead of mysql_db_query(). are you connecting to same DB
mysql_db_query() is deprecated.
so try this:
PHP Code:

while ($row mysql_fetch_array($data))

   {
      
      
$fetch mysql_query("SELECT name FROM realname WHERE uid='$row[article_id]' LIMIT 1")or
      die(
"Select Failed!");
      
$realname mysql_fetch_assoc($fetch);
          
      
$writers[] = $realname['name'];
   }

$art implode(', '$writers);

$tpl->assign('comic_writer'$art); 
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 05-06-05 at 04:21 PM.
Reply With Quote
  #5 (permalink)  
Old 05-06-05, 04:52 PM
Programme Programme is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Ah, there we go. Things looking good. My script uses mysql_db_query alot. Should i rewrite and use mysql_query instead?
Reply With Quote
  #6 (permalink)  
Old 05-07-05, 07:17 AM
kenetix kenetix is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Programme
Ah, there we go. Things looking good. My script uses mysql_db_query alot. Should i rewrite and use mysql_query instead?
i think theyre about the same, one is probably the same function with a newer name.
__________________
http://KENETIX.NET - FREE MONEY MAKING TOOLS & RESOURCES
Reply With Quote
  #7 (permalink)  
Old 05-07-05, 12:57 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
Originally Posted by Programme
Ah, there we go. Things looking good. My script uses mysql_db_query alot. Should i rewrite and use mysql_query instead?
it's better to replace them.
this is a quote from PHP manual:
Quote:
Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.
besides that you are only using one DB, so there is no need to reselect the same DB each time you execute a query.
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #8 (permalink)  
Old 05-08-05, 09:40 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Why don't just use table join to select all the writers in a single query?
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
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
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
Return String values from a object saved in an array mr_wazzup Everything Java 1 04-15-05 09:11 AM
How to store multiple values in a string without overriding the pervious one bamboat_3 ASP.NET 3 08-26-04 09:08 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


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