I have an array in the following format.
$data = array(
1=>array('img'=>'1.png','title'=>'title1','desc'=>'desc1'),
2=>array('img'=>'2.png','title'=>'title2','desc'=>'desc2'),
1=>array('img'=>'3.png','title'=>'title3','desc'=>'desc3'),
);
Here is the final output I need,
<img src="1.png">
<h1>title1</h1>
<p>desc1</p>
<img src="2.png">
<h1>title2</h1>
<p>desc2</p>
.........
How can i create it? Thanks for the help.
Use a foreach loop, like so:
foreach( $data as $item) {
echo '<img src="' . $item['img'] . '">';
echo '<h1>' . $item['title'] . '</h1>';
echo '<p>' . $item['desc'] . '</p>';
echo "\n";
}
Simply use your array like
foreach($data as $item){
echo $item['title'];
.....
}
This will give you the logic. Now you can apply the img and tags properly
<?php
foreach($data as $item) {
?>
<img src="<?=$item['img']?>">
<h1><?=$item['title']?></h1>
<p><?=$item['desc']?></p>
<br />
<?php } ?>
Another option here.
Related
I'm getting category from my database using PHP and I want to use it inside Google Analytics event tracking code. The problem is that events are not being recorded in Google Analytics.
Here are some code snippets from my project:
1)
$docs = array();
while ($row = mysql_fetch_assoc($result)) {
$doc = array();
$doc['my_tel'] = $row['my_tel'];
$doc['my_category'] = $row['my_category'];
$docs[] = $doc;
}
mysql_free_result($result);
2)
<?php
$html_output = '';
foreach ($docs as &$d) {
$html_output .= '<div>' .
'<img src="call.png" width="65px" height="35px" border="0">' .
'</div>';
}
echo $html_output;
?>
If you have a look at the console of your browser there might be a JavaScript error, because the second parameter of _gaq.push is not wrapped with quotes.
Try this:
<?php
$html_output = '';
foreach ($docs as &$d) {
$html_output .= '<div>' .
'<img src="call.png" width="65px" height="35px" border="0">' .
'</div>';
}
echo $html_output;
?>
Looking at your append to $html_output, it appears you're not enclosing the category within single quotes in the resultant javascript output.
Assuming for example my_tel was 01234567890 and category was 'Category 1' the output from the code above would be:
<div><img src="call.png" width="65px" height="35px" border="0"></div>
Perhaps try (note additional escaped quotes around category:
'<a href="tel:' . $d['my_tel'] . '" onClick="_gaq.push([\'_trackEvent\', \'' . $d['my_category'] . '\', \'Event Action\',...
--dan
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $menu[$key] . '">' . $menu[$key] . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu[1]); ?>
What i basically want to do is to pass a specific array value when i'm echoing out the menu.
I'm building a single page website with anchors and i want to pass value's so i can echo out the "top"-link.
I'm stuck at the point on how to pass the $key value trough the function.
**edit: I'm trying to print specific links. I want a function that is able to print out an link but i want to specify the link to print via the function argument.
for example:
<?php echo main_menu($key = '0'); ?>
result:
prints url: top
<?php echo main_menu($key = '2'); ?>
result:
prints url: photography
**
(A lack of jargon makes it a bit harder to explain and even harder to google.
I got my books in front of me but this is taking a lot more time than it should.)
You either need to pass the entire array and loop, or pass a single array item and not loop:
Single Item:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
$return .= '<a class="menu" href="index.php#' . $menu . '">' . $menu . '</a>' . PHP_EOL .'';
$return .= '</div>';
return $return;
}
echo main_menu($menu[1]);
Entire Array:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach($menu as $value) {
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
echo main_menu($menu);
You don't need $menu[$key] just use the $value.
Should you not just be using $value inside your loop? And passing the entire array rather than one item of the $menu array?
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu); ?>
Try:
echo main_menu($menu); // You will get your links printed
Instead of
echo main_menu($menu[1]); // In this case error is occured like : **Invalid argument supplied for foreach**
NOTE: You can use $value instead of $menu[$key]
I am trying to extract download links from a site. However i only get the last item in my array.
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
foreach ($dllinks as $value)
{
echo $value . '<br>';
}
?>
When I use var_dump it shows all the download links in my array. But for some strange reason it only shows the last item in the second foreach loop.
EDIT:
Sorry it was supposed to be like this
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
?>
I kept this verbose so its easier to see whats going on... Basically, grab each row.. Find the name and the link from the row. spit it out..
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('.tlistrow') as $row){
$link_nodes = $row->find('td.tlistdownload a');
$name_nodes = $row->find('td.tlistname a');
if (count($link_nodes) > 0 && count($name_nodes) > 0) {
$link = $link_nodes[0]->href;
$name = htmlentities($name_nodes[0]->innertext);
echo "<a href='{$link}'>{$name}</a>\n";
}
}
I am trying to get two foreach loop by explode with two delimiters <> and "\n" but getting error. Warning: Invalid argument supplied for foreach()
Here is my code
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
$descs = explode("\n", $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';
?>
The value entering into textarea something like this Title here<>this is the first scooter ever made.
Title here 2<>another line for specification In fact I would like to make it like <title 1> here detail text
Thanks a lot
actually you should do something like this
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$descs = explode("\n", $specifications);
echo '<dl>';
foreach($descs as $desc){
$title = explode('<>', $desc);
echo '<dt>' . $title[0] . '</dt>';
for($i=1; $i<=count($title); $i++){
echo '<dd>' . $title[$i] . '</dd>';
}
}
echo '</dl>';
?>
The $descs variable isn't an array because the first foreach loop sets $descs.
See this line :
foreach($titles as $title => $descs){
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
$descs = explode("\n", $descs);
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';
here is my working php explode code for NON links:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li>' . $item . '</li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
..and the code which defines "my_custom_output", which I input into my textarea field:
text1,text2,text3,etc
..and the finished product:
text1
text2
text3
etc
So that works.
Now what I want to do is make text1 be a link to mywebsite.com/text1-page-url/
I was able to get this far:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li class="link-class"><a title="' . $item . '" href="http://mywebsite.com/' . $item_links . '">' . $item . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
Now, I would like $item_links to define just the rest of the url. For example:
I want to put input this into my textarea:
text1:text1-page-url,text2:new-text2-page,text3:different-page-text3
and have the output be this:
text1
text2
..etc (stackoverflow forced me to only have two links in this post because I only have 0 reputation)
another thing I want to do is change commas to new lines. I know the code for a new line is \n but I do not know how to swap it out. That way I can put this:
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
I hope I made this easy for you to understand. I am almost there, I am just stuck. Please help. Thank you!
Just split the $item inside your loop with explode():
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And choose your string separators so $item_text, $item_links wouldn't contain them.
After you explode the string you could loop through again and separate the text and link into key/values. Something like...
$array_final = new array();
$array_temp = explode(',', $textarea);
foreach($array_temp as $item) {
list($text, $link) = explode(':', $item);
$array_final[$text] = $link;
}
foreach($array_final as $key => $value) {
echo '<li>'.$key.'</li>';
}
to change comma into new line you can use str_replace like
str_replace(",", "\r\n", $output)