timmah1 Posted January 27, 2009 Share Posted January 27, 2009 I know this is probably simple to do, but I cannot figure it out. I want to pull info from the database, put it into an array, then separate them with a comma. Here is what I've done, this shows everything it's suppose to, but the categories are not separated. <?php $subname = array("$row[name]"); $words = implode(", ", $subname); for($i = 0; $i < count($words); $i++){ echo "<span class='small-links'>"; echo "$subname[$i]"; echo "</span>"; } ?> Can anybody help me out? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/142674-solved-probably-simple/ Share on other sites More sharing options...
.josh Posted January 27, 2009 Share Posted January 27, 2009 In your code, you are making an array ($subname) with only one element, effectively making it the same as a regular string variable. You can rewrite all of that code simply by doing $subname = "<span class='small-links'>{$row['name']}</span>"; But you say you have a array of info, so I don't think that's quite what you are looking for. Perhaps you should post the code before that, that retrieves the info from the database. Quote Link to comment https://forums.phpfreaks.com/topic/142674-solved-probably-simple/#findComment-747832 Share on other sites More sharing options...
timmah1 Posted January 27, 2009 Author Share Posted January 27, 2009 I'm just trying to separate each sub_cat by a comma, without having the last one have a comma as well I know I've done this before, but I can't figure it out, or remember. I'd like to have it show like this: cat1, cat2, cat3, cat4... right now it shows like this: cat1cat2cat3cat4 <?php $sql = "SELECT * FROM suns WHERE main_cat = '$catid'"; $res = mysql_query($sql); while($row = mysql_fetch_assoc($res)){ $subid = $row['id']; $subname = array("$row[name]",); $words = implode(", ", $subname); for($i = 0; $i < count($words); $i++){ echo "<span class='small-links'>"; echo "$subname[$i]"; echo "</span>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142674-solved-probably-simple/#findComment-747841 Share on other sites More sharing options...
Philip Posted January 27, 2009 Share Posted January 27, 2009 <?php $sql = "SELECT * FROM suns WHERE main_cat = '$catid'"; $res = mysql_query($sql); while($row = mysql_fetch_assoc($res)){ $subid[] = $row['id']; $subname[] = $row['name']; /* for($i = 0; $i < count($words); $i++){ echo "<span class='small-links'>"; echo "$subname[$i]"; echo "</span>"; } }*/ $words = implode(", ", $subname); echo $words; // this will list "cat1, cat2, cat3" ?> Quote Link to comment https://forums.phpfreaks.com/topic/142674-solved-probably-simple/#findComment-747846 Share on other sites More sharing options...
timmah1 Posted January 27, 2009 Author Share Posted January 27, 2009 This is doing what I want it to do, but now it's putting every sub-category under all the categories Quote Link to comment https://forums.phpfreaks.com/topic/142674-solved-probably-simple/#findComment-747871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.