almightyegg Posted December 24, 2007 Share Posted December 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/ Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 $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? Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422631 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 I tried running the script Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/lordofth/public_html/society/bob.php on line 8 Line 8: $text .= $value " ,"; Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422632 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 $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 . " ,"; Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422635 Share on other sites More sharing options...
Ken2k7 Posted December 24, 2007 Share Posted December 24, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422639 Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422644 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422653 Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 So it works? Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422655 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 No, it says 'Array, ' instead of he list Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422657 Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422658 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 Now it did this: Lord of the Abyss gave Array, Lord of the Abyss gave Array, to Rulers of the Abyss. Reason: Yeha Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422661 Share on other sites More sharing options...
papaface Posted December 24, 2007 Share Posted December 24, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422663 Share on other sites More sharing options...
kenrbnsn Posted December 24, 2007 Share Posted December 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422664 Share on other sites More sharing options...
almightyegg Posted December 24, 2007 Author Share Posted December 24, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422668 Share on other sites More sharing options...
corbin Posted December 25, 2007 Share Posted December 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422711 Share on other sites More sharing options...
almightyegg Posted December 25, 2007 Author Share Posted December 25, 2007 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422843 Share on other sites More sharing options...
almightyegg Posted December 25, 2007 Author Share Posted December 25, 2007 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422859 Share on other sites More sharing options...
kenrbnsn Posted December 25, 2007 Share Posted December 25, 2007 Please dump the the array "$vals": <?php $vals = array($i1,$i2,$i3,$i4,$i5); echo '<pre>' . print_r($vals,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422901 Share on other sites More sharing options...
almightyegg Posted December 25, 2007 Author Share Posted December 25, 2007 Ah it was an error on my part, I'd put an entire Array into $i1-5 intead it is $i1-5[name] All done Quote Link to comment https://forums.phpfreaks.com/topic/83078-solved-writing-a-list-out/#findComment-422951 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.