- This topic is empty.
-
AuthorPosts
-
May 17, 2014 at 6:51 pm #170510
Jamie
ParticipantI need some help with some php code. I am using Option tree with WordPress. They have a social option in the back end where you can press a button to add a new option. I have two options right now. When I do print_r on the variable, I get this
Array ( [0] => Array ( [name] => Facebook [title] => [href] => )
[1] => Array
(
[name] => Twitter
[title] =>
[href] =>
)
)
`My problem is that I am not sure of the most efficient way to use this information. I thought about trying to put it in a switch statement. But I am not sure how big it could or would be. Each one would need to be echoed in an unordered list because each list item would have an icon associated with it.
May 17, 2014 at 8:08 pm #170511chrisburton
ParticipantI’m not really understanding your problem. What are you expecting to happen with the array?
May 17, 2014 at 9:13 pm #170513__
ParticipantLikewise. But from what I can take away from it, a
switch
is not what you need. Can you describe the HTML markup do you want to end up with?May 17, 2014 at 9:21 pm #170515Jamie
ParticipantThere needs to be some loop that will pick up when a new option is added in the back end. It will output this
<li><a href="facebook.com"><img src="facebook.jpg">Facebook</a>
So then if a Twitter option was added in the back end, the code would output a list item for twitter. So I need to read the array and cycle through the index to get what I need.
May 18, 2014 at 11:21 am #170535__
ParticipantWhen you want to make sure you do something with every element of an array, use
foreach
.<?php foreach( $array as $item ){ /* do something to each item in array */ }
e.g.,
<?php $LIs = ""; foreach( $yourArray as $item ){ $LIs .= <<< HTML <li> <a href="{$item['href']}"> <img src="{$item['img']}"> {$item['name']} </a> </li> HTML; } print "<ul>$LIs</ul>";
May 18, 2014 at 11:52 am #170545Jamie
ParticipantI have tried that and I get an error. Array to string conversion.
May 18, 2014 at 12:33 pm #170548__
ParticipantCan you show us the actual code you are working with (use a service like pastebin or make a gist on github if there is too much code to post here)?
May 19, 2014 at 9:51 am #170605shaneisme
Participant$LIs .= <<< HTML <li> <a href="{$item['href']}"> <img src="{$item['img']}" /> {$item['name']} </a> </li> HTML;
Is this a method of doing HTML inside PHP that I don’t know about, or is this pseudocode? I tried doing some Googling on it and couldn’t find anything.
May 19, 2014 at 1:35 pm #170618__
ParticipantSorry; it’s called a heredoc.
I’ve developed a habit of using them, as it makes it very easy to write large blocks of HTML (since you can use variables, single, and double-quotes).
May 19, 2014 at 1:47 pm #170619shaneisme
ParticipantAwesome! New toy, thanks :)
May 19, 2014 at 7:50 pm #170638chrisburton
ParticipantI think it’s harder to read (as a beginner)
May 19, 2014 at 8:28 pm #170642__
ParticipantI think it’s harder to read (as a beginner)
It certainly is harder to figure out, especially for beginners. (That’s why I said sorry for using it: it’s not the best choice for example code.) But once you understand how to use the delimiters (i.e.,
<<< SOME_WORD
andSOME_WORD;
), it gets very quick and convenient. -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.