PDA

View Full Version : adding conditionals in an array


sabret00the
04-08-2006, 06:37 PM
is it possible at all, i'm trying to do something like this

$this->entrybit = str_replace(
array(
'{QUICKENTRYLINK}',
'{TITLE}',
'{DATE}',
'{TIME}',
($entry['security'] != "noone") ? "'{LOCKEDICON}'," : "")
'{USERICON}',
'{ENTRYTEXT}',
'{MOOD}',
'{MUSIC}',
'{TAGS}',
'{COMMENT}',
'{READ}',
'{LINK}',
'{REMEMBER}',
'{TOP}'
),
array(
$entry['journalid'] . '_' . $entry['entryid'],
$entry['title'],
vbdate($this->vbulletin->options['dateformat'], $entry['entry_date']),
vbdate($this->vbulletin->options['timeformat'], $entry['entry_date']),
($entry['security'] != "noone") ? "'<img src=\"' . $this->stylevar['imgdir_misc'] . '/moderated.gif\" alt=\"' . ucwords(strtolower($entry['security'])) . '\" />'," : "")
$entry['usericon'],
$entry['pagetext'],
'<b>Mood:</b> ' . ucwords(strtolower($entry['mood'])) . ' <img src="' . buildmoodimage($entry['mood'], $entry['location']) . '" />',
'<b>Music:</b> ' . ucwords(strtolower($entry['music'])),
'<b>Tags:</b> ' . buildtaglinks($entry['tags'], $this->userdetails['username']),
'Reply',
$entry['commentcount'] . ' Comments',
'Perma-Link',
'Memorise',
'<a href="#top">Top</a>'
),
$this->entrybit
);

note: this one don't work.

Code Monkey
04-08-2006, 11:46 PM
Create variables with the values where you have conditionals, then just put the variables in the arrays.

Then put the array variables in the string replace.

Also, those conditionals where the else = "" are not necessary. Just put the values in. If it's not there, there is nothing to replace and it will move on.

So for example.

($entry['security'] != "noone") ? "'{LOCKEDICON}'," : "")


Should just be

"'{LOCKEDICON}',"

sabret00the
04-10-2006, 10:19 AM
thanks for the help, i ended up with this

$this->entrybit = str_replace(
array(
'{QUICKENTRYLINK}',
'{TITLE}',
'{DATE}',
'{TIME}',
'{LOCKEDICON}',
'{USERICON}',
'{USERNAME}',
'{ENTRYTEXT}',
'{MOOD}',
'{MUSIC}',
'{TAGS}',
'{COMMENT}',
'{READ}',
'{LINK}',
'{REMEMBER}',
'{TOP}'
),
array(
$entry['journalid'] . '_' . $entry['entryid'],
$entry['title'],
vbdate($this->vbulletin->options['dateformat'], $entry['entry_date']),
vbdate($this->vbulletin->options['timeformat'], $entry['entry_date']),
($entry['security'] != 'everyone') ? '<img src="' . $this->stylevar['imgdir_misc'] . '/moderated.gif" alt="' . ucwords(strtolower($entry['security'])) . '" />' : '',
$entry['usericon'],
$this->userdetails['username'],
$entry['pagetext'],
($entry['mood']) ? '<b>Mood:</b> ' . ucwords(strtolower($entry['mood'])) . ' <img src="' . buildmoodimage($entry['mood'], $entry['location']) . '" />' : '',
($entry['music']) ? '<b>Music:</b> ' . ucwords(strtolower($entry['music'])) .'' : '',
($entry['tags']) ? '<b>Tags:</b> ' . buildtaglinks($entry['tags'], $this->userdetails['username']) .'' : '',
'Reply',
'<a href="entry.php?e=' . $entry['entryid'] . '">' . $entry['commentcount'] . ' Comments</a>',
'<a href="entry.php?e=' . $entry['entryid'] . '">Perma-Link</a>',
'Memorise',
'<a href="#top">Top</a>'
),
$this->entrybit
);