Jump to content

[SOLVED] Comma Seperaters


xXREDXIIIXx

Recommended Posts

OK heres the problem:

 

I am working on my video games section. you all probably know the some games ore only on certain platforms and some are on a few or all,

 

I would like to show which platforms the game is on (I can achive this) BUT with comma seperaters.

I have tried using implode but if one of the vars is empty it produces something like:

 

Playstation3, , Xbox 360, ,

 

So I need a way of either removing the excess commas or a way around it

 

Please help :)

Link to comment
Share on other sites

Thanks for quick reply I love this place :)

 

here was the code

 

if($_SESSION['by'] == 'games'){		

if($row['ps3'] == 1){$ps3 = "Playstation 3";}
if($row['xbox'] == 1){$xbox = "Xbox";}
if($row['360'] == 1){$x360 = "Xbox 360";}
if($row['wii'] == 1){$wii = "Wii";}
if($row['pc'] == 1){$pc = "Windows";}

$array = array($ps3, $xbox, $x360, $wii, $pc);
$comma_separated = implode(", ", $array);

$comma = str_replace(", ,", ",", $comma_separated);
echo str_replace(", ,", ",", $comma);
}

 

I had to do the str_replace twice once wasnt enough  ::)

 

 

---

 

reply to new post could you show me how to implament that into my above? I better understand new code when I see with my own words so to speak.

 

Link to comment
Share on other sites

3 tables

 

table 1 - platformID platformName

 

table 2 - gameID gameName ...

 

table 3 - gameID platformID

 

table 3 will have foreign keys that relate to the primary keys of the other tables.

 

query on game id selecting platfrom ids that match in table 3 and so get the corsponding platform name from platform id.

 

Put results in an array and then implode(', ', $arr);

 

Job done.

Link to comment
Share on other sites

3 tables

 

table 1 - platformID platformName

 

table 2 - gameID gameName ...

 

table 3 - gameID platformID

 

table 3 will have foreign keys that relate to the primary keys of the other tables.

 

query on game id selecting platfrom ids that match in table 3 and so get the corsponding platform name from platform id.

 

Put results in an array and then implode(', ', $arr);

 

Job done.

 

errm got it wroking with the previous post, thanks anyway  8)

 

if($_SESSION['by'] == 'games'){		

if($row['ps3'] == 1){$ps3 = "Playstation 3";}
if($row['xbox'] == 1){$xbox = "Xbox";}
if($row['360'] == 1){$x360 = "Xbox 360";}
if($row['wii'] == 1){$wii = "Wii";}
if($row['pc'] == 1){$pc = "Windows";}

$array = array($ps3, $xbox, $x360, $wii, $pc);

foreach($array as $key => $value) {
  if($value == "") {
unset($array[$key]);
  }
}
$consoles = implode(", ", $array);

echo $consoles;
}

 

This can be marked as solved thanks :)

Link to comment
Share on other sites

OK, instead of creating the array with items you don't want how about you only put the item into

the array if it tests true (==1) using array_push

 

http://us.php.net/array_push

 

This should work

 

if($_SESSION['by'] == 'games') {

# Create the Array
$array = array();      
                              
if($row['ps3'] == 1) {
  array_push($array, "Playstation 3");
}

if($row['xbox'] == 1) {
  array_push($array, "Xbox");
}

if($row['360'] == 1) {
  array_push($array, "Xbox 360");
}

if($row['wii'] == 1) {
  array_push($array, "Wii");
}

if($row['pc'] == 1) {
  array_push($array, "Windows");
}
               
$comma_separated = implode(", ", $array);
               
echo $comma_separated;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.