Jump to content

Recommended Posts

Yeah, I'm stuck...again...

 

basically I want to write a list like

 

You gave $item1, $item2, $item3, $item4, $item5 to such and such...

 

But The variables aren't always full, so the the list could be:

 

You gave $item2, $item4, $item5 to such and such...

 

OR

 

You gave $item1, $item5 to such and such...

 

How would I do smething like this?? Any ideas greatly received :)

Link to comment
https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/
Share on other sites

$i = 1;
$text = 'You gave';
$name = 'item' . $i;
while(isset($$name)) {
    $text .= ' ' . $$tmp . ',';
    $i++;
    $name = 'item' . $i;
}
$text = substr($text, -1);
//not sure if  substr or a rtrim would be faster

 

You could do that if you didn't want to make an array....  But, I suggest using an array.

 

 

 

P.S.:

 

Line 8:  $text .= $value " ,";

 

That should be

Line 8:  $text .= $value . " ,";

$list = array("fjdsii","jgf","",098r3","");
$text = "You gave";
foreach ($list as $value)
{
if (trim($value) != "")
  {
  $text .= " ". $value . ",";
  }
}
$text = trim($text,",");
echo $text;

Untested, is that what you want?

Correction:

foreach ($value as $list)

It should work with that correction.

$list = array("fjdsii","jgf","",098r3","");
$text = "You gave";
foreach ($list as $value)
{
if (trim($value) != "")
  {
  $text .= " ". $value . ",";
  }
}
$text = trim($text,",");
echo $text;

Untested, is that what you want?

Correction:

foreach ($value as $list)

It should work with that correction.

That wont work at all.

to the OP:

You copied my code before I edited it to correct it a few seconds after I posted lol.

It worked when I tested it outside of my code now when I inserted it within the rest of my own it has $text:

24-12-2007 17:57:31 Lord of the Abyss gave Array, to Rulers of the Abyss.

 

$list = array("$i1","$i2","$i3","$i4","$i5");
$text = "<a href='/player/view.php?id=$mem[id]'>$mem[username]</a> gave ";
foreach ($list as $value)
{
if (trim($value) != "")
  {
  $text .= $value . ", ";
  }
}
$text = trim($text,",");
if(!$reason){
$text = "" . $text . "to $clutch[name].";
}else{
$text = "" . $text . "to $clutch[name]. Reason: $reason";
}

Should be:

$list = array("$i1","$i2","$i3","$i4","$i5");
$text = "<a href='/player/view.php?id=$mem[id]'>$mem[username]</a> gave ";
foreach ($list as $value)
{
if (trim($value) != "")
  {
  $text .= $value . ", ";
  }
}
$text = trim($text,",");
if(!$reason){
$text .= "" . $text . "to $clutch[name].";
}else{
$text .= "" . $text . "to $clutch[name]. Reason: $reason";
}

Try:

$list = array($i1,$i2,$i3,$i4,$i5);
$text = "<a href='/player/view.php?id=$mem['id']'>$mem['username']</a> gave ";
foreach ($list as $value)
{
if (trim($value) != "")
  {
  $text .= $value . ", ";
  }
}
$text = trim($text,",");
if(!$reason){
$text .= "" . $text . "to $clutch['name'].";
}else{
$text .= "" . $text . "to $clutch['name']. Reason: $reason";
}

You really should learn how to deal with arrays and the many array based functions. My solution uses the array_filter() function:

<?php
function filter_blank($val) {
return ($val != '');
}

$vals = array("fjdsii","jgf","","098r3","");
echo 'This list contains ' . implode(', ',array_filter($vals, "filter_blank")) . '<br>';

?>

 

Ken

Thanks for the input but I'd rather just get this working other wise I'll just have to start over with another code.

 

I made a few alterations myself, it now shows:

Lord of the Abyss gave Array, to Rulers of the Abyss. Reason: Yeha

$list = array("$i1","$i2","$i3","$i4","$i5");
$text = "<a href='/player/view.php?id=" . $mem['id'] . "'>" . $mem['username'] . "</a> gave ";
foreach ($list as $value)
{
if (trim($value) != "")
  {
  $text .= $value . ", ";
  }
}
$text = trim($text,",");
if(!$reason){
$text .= "to " . $clutch['name'] . ".";
}else{
$text .= "to " . $clutch['name'] . ". Reason: $reason";
}

Actually, if you're going to do it with an array (which I think you should), ken's way is probably the best.

 

It's pretty simple too.

 

The implode takes each element and puts , between it.  The array_filter goes through each element and passes it to filter_blank().  If filter_blank() returns true, then it adds it to the return array, which then will be imploded.

 

filter_blank sees if it's empty, and if the value isn't empty, it returns true.

I tried Ken's way and got $mem[username] gave Arrayto $clutch[name]. Reason: $reason

 

function filter_blank($val) {
return ($val != '');
}

$vals = array("$i1","$i2","$i3","$i4","$i5");

if(!$reason){
$text = '<a href=/player/view.php?id=$mem[id]>$mem[username]</a> gave ' . implode(', ',array_filter($vals, "filter_blank")) . 'to $clutch[name].';
}else{
$text = '<a href=/player/view.php?id=$mem[id]>$mem[username]</a> gave ' . implode(', ',array_filter($vals, "filter_blank")) . 'to $clutch[name]. Reason: $reason';
}

I changed a few bits, it displays: Lord of the Abyss gave Array to Rulers of the Abyss.

it says array instead of a list

 

function filter_blank($val) {
return ($val != '');
}

$vals = array("$i1","$i2","$i3","$i4","$i5"); // I think it may be to do with the way the Array is set up

if(!$reason){
$text = '<a href=/player/view.php?id=' . $mem[id] . '>' . $mem[username] . '</a> gave ' . implode(', ',array_filter($vals, "filter_blank")) . ' to ' . $clutch[name] . '.';
}else{
$text = '<a href=/player/view.php?id=' . $mem[id] . '>' . $mem[username] . '</a> gave ' . implode(', ',array_filter($vals, "filter_blank")) . ' to ' . $clutch[name] . '. Reason: $reason';
}

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.