Jump to content

[SOLVED] Probably simple


timmah1

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/142674-solved-probably-simple/
Share on other sites

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.

 

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>";
}
}
?>

<?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"
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.