PHP Code Within Element Causes Repetition - php

I'm finding that placing php code snippets within my website's & tags is causing a problem with the code after it's placement. I first noticed it when placing avatars on my site in a list format where it works fine up to a point and then the same image is repeated for the rest of the list. Here is the code that I'm uisng for that:
<?php
$show_user .= "
<div class=\"section\">
<div class=\"sectionInner\">
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/$member_avatar\"></div>
<div class=\"searchInformation\"><div class=\"searchInformationPrimary\">$member_name</div><div class=\"searchInformationSecondary\"><i>"$member_summary"</i></div></div>
<div class=\"searchInformation\"><div class=\"searchInformationPrimary\">$member_subtype $member_type</div><div class=\"searchInformationSecondary\">$member_city, $member_county</div><div class=\"searchInformationThird\">View Details</div></div>
<div class=\"clearLeft\"></div>
</div>
</div>
<div class=\"searchResultSplitter\"></div>
";
?>
<?php echo $show_user; ?>
I'm now noticing it when placing php within my navigation bar where it's not closing the tag. Here is my code for the bar:
<div id="pageSubNavigation" class="page<?php echo $thispage; ?>SubNavigation">
Next Page
</div>
After this, all of the other tags show the same link. Any ideas why this might be?

You need to either jump out of the HTML to add the variables or wrap them in {} so they appear correctly.
Example:
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/".$member_avatar."\"></div>
or
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/{$member_avatar}\"></div>
Check out this article about string formatting with variables in PHP.

Related

Replacing content between two div tags - PHP

I'm trying to replace content between two div tags using str_replace but I'm unsure how.
The content will be
<div class="profile-details">
<div class="username">Paradigm</div>
<div class="dob">01/01/2015</div>
</div>
What I want to do is replace the content between the <div class="profile-details">content </div> tags. The content is variable depending on the user profile.
Assuming this is a HTML string.
This would be my approach
echo preg_replace('/<div class="username">.+?</div>/im', '<div class="username">Special Username<\/div>', $string) ;

php in foundation modal

I am trying to pull some information from my database and put it in a modal. I went to the foundations website and tried to figure it out from their docs section. I dont exactly understand it. So I have a section of my site that allows users to request to delete a song they uploaded. Now if they click the X a modal should pop up and ask to confirm.
<div class="row">
<div class="large-8 column musicup">
<p> <?php echo "No music uploaded..."; ?> </p>
</div>
</div>
<?php
}else{
?>
<h2 style="margin-top:1em;">Music uploaded</h2>
<hr style="opacity:.4;">
<?php
while($row_a = mysql_fetch_array($res))
{
?>
<div class="row">
<div class="large-4 column musicup">
<p><?php echo $row_a['title']; ?></p>
</div>
<div class="large-3 column musicup"><span data-tooltip class="has-tip tip-top" title="<?php echo $row_a['reason']; ?>">
<div class="button <?php echo $row_a['status'];?>"><?php echo $row_a['status'];?></div>
</span></div>
<div class="large-3 column musicup_date">
<p><?php echo date('F j Y',strtotime($row_a['uploaded'])); ?> </p>
</div>
<div class="large-2 column musicup">
<p>X</p>
</div>
</div>
<?php
}
}
}
?>
</div>
So now I have the modal and all the database queries on a new page called song_delete.php.
Here is the code for that:
<?php
include_once "functions.php";
$query = sprintf("SELECT * FROM songs WHERE user_id = %d AND song_id = %d",$_SESSION['user_id'], $_GET['id']);
$res = mysql_query($query) or die('Error: '.mysql_error ());
$row_a = mysql_fetch_assoc($res);
$totalRows_a = mysql_num_rows($res);
?>
<div id="deleteMusic" class="reveal-modal medium">
<h2>Request to delete<span style="color:#F7D745;"> <?php echo $row_a['title']; ?></h2>
<p class="lead">Are you sure you want to delete this song? Please allow 2 full business weeks for deletion.</p>
<span style="float:right;">Cancel
Submit </span>
<a class="close-reveal-modal">×</a>
</div>
Thanks for any help in advance. I appreciate it.
Please dont tell me about the mysql_query and how I should use PDO or MySQLi and OOP i know this, but this site is not currently coded with all that..
OK first things first - its often better to look at the compile source (HTML Source Code) in these cases. Can you do this? From the code you've given it looks fine but without the css/js linking and showing the placement of the reveal code there's no way to tell.
How Foundation Reveals Work
1 - The modal code is placed just before the ending </body>.
2 - It should look something like this:
<div id="myModal" class="reveal-modal">
<h2>Awesome. I have it.</h2>
<p class="lead">Your couch. It is mine.</p>
<p>Im a cool paragraph that lives inside of an even cooler modal. Wins</p>
<a class="close-reveal-modal">×</a>
</div>
3 - Depending on the size you want you can use an extra class of .small (for a reveal size of 30% browser width. Or one of these (taken directly from Foundation Docs)
.medium: Set the width to 40%.
.large: Set the width to 60%.
.xlarge: Set the width to 70%.
.expand: Set the width to 95%.
4 - At this point you can attach data-reveal-id="<id of modal here>" or call the modal via foundation. At this point your modal will popup in all Foundation 4 supported browsers. However you need the javascript files to close it.
5 - Now make sure you have the necessary scripts
<!-- If running version with default scripts -->
<script src="foundation.js"></script>
<script src="foundation.reveal.js"></script>
6 - Then call $(document).foundation() and then via the magical jQuery javascript library it should work as intended :-).
Extras
You can add extras attributes to reveal if you wish this way (List of all the attributes
):
$(document).foundation('reveal',<options here>,<callback>)
Lastly you might want to take the ajax tag off this (you aren't calling any content in asynchronously - it's all compiled at runtime via your server

file_get_html() fetching plain text inside a div, but avoiding all other tags

i am using file_get_html() to get some external HTML, but i have a issue. I cannot seem to target text inside a div, while avoiding getting the rest of the content.
Lets say the layout is this:
<div class="post">
<h1>Andromeda v1.4 – WordPress – The Beauty of Simplicity</h1>
<div class="infos b20">
<img class="post_img" src="/imagini/512b93babf84b.jpg" alt="Andromeda v1.4 – WordPress – The Beauty of Simplicity">
<div style="width:610px; margin:10px 0; overflow:hidden; display:block;">
enter code here
Andromeda is a clean theme with functional CMS and unique features. A massive pack of backend CMS options was created for this product to give you full control while creating and editing the site and its features. The main idea behind this theme was to create a something clean and simple, useful, nice looking and easy to modify.
<p></p>
<h6>Demo</h6>
<code>http://themeforest.net/item/andromeda-wordpress-the-beauty-of-simplicity/107876</code>
<h6>Download:</h6>
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<p></p>
<ul id="social_post" class="clearfix sharingbtns">
<div class="comments">
</div>
If i do a
$text = $dom->find('div[class=post]');
$text = $text[0]->plaintext;
I get all the content, I only want the text, inside the main div with the class post, and not all the other content.
What would be the best way to achive this?
Text and amount of other divs are variable, but the div class post, and the text will always be there, in the same position.
EDIT: To elaborate, i only want the text thats inside post, and has no tag
just to answer you quickly without checking out if it works:
http://simplehtmldom.sourceforge.net/manual_api.htm
Try this:
$text = $dom->find('div[class=post]');
$text = $text[0]->innertext;
or:
$text = $dom->find('div[class=post]');
$text = $text[0]->outertext;
By the way:
<div style="width:610px; margin:10px 0; overflow:hidden; display:block;">
has no closing tag so there is no text that's inside the DIV you;re talking about. Please clarify.
$res = $html->find('div[class=post]',0)->plaintext;

PHP Syntax Error? - Create a MultiDimentional Array, Use a foreach loop - output HTML

QUESTION ANSWERED:
After messing around with a few things, The combination of the comma at the and of my last array element, and not having the image source path typed correctly was the problem.
Everything works great. It never ceases to amaze me at how a simple syntax error can throw everything out of whack.
Thank you Jonathan and tmsimont! The combo of you're answers fixed my problem.
Time to get a good PHP debugger, Netbeans probably, because Dreamweaver just isn't cutting it for me and I'm not versed enough to hand code YET!
EDIT:
I'm going to try and make myself a little more clear by showing my output source:
<body>
<div id="main">
<div class="container">
<div class="sponsorInner">
<a href="'.$company[1].'">
<img src="img/sponsors/'.$company[0].'.png">
</a>
</div>
</div>
'; } ?>
</div>
</body>
3 things not working:I need the first div with the class gold to render, and the $'s from the array to populate the image link and path.
ORIGINAL QUESTION:
I am trying to create multiple div arrangements on a page using a multidimentional array and a foreach loop to populate the div contents with a an image mnemonically named in a relative path folder and the image to be a hyperlink.
My output is not displaying the first correctly, and not populating the array $'s.
This seems right to me, am I missing some type of escape or syntax?
<body>
<?php
$sponsor = array(
array('Allstate','http://www.allstateagencies.com/'),
array('Arrowhead','http://www.arrowheadwater.com/'),
);
?>
<div id="main">
<div class="container">
<?php
foreach($sponsor as $company)
{
echo '
<div class="gold">
<div class="sponsorInner">
<img src="img/sponsors/'.$company[0].'.png"/>
</div>
</div>
';
}
?>
</div><!--.container-->
</div><!--#main-->
</body>
A total guess.. since that's all I can do with the way you've phrased your question... but if your problem is with the image output, try putting a "/" in beginning the src attribute of the image:
<img src="/img/sponsors/'.$company[0].'.png"/>
that way the browser looks for the img directory from the base of the site, instead of from within whatever subdirectory you're in.
Why not do your array this way
$sponsor = array(
'Allstate' => 'http://www.allstateagencies.com/',
'Arrowhead' => 'http://www.arrowheadwater.com/'
);
and later
<?php
foreach($sponsor as $company_name => $company_site)
{
?>
<div class="gold">
<div class="sponsorInner">
<img src="img/sponsors/<?php echo $company_name ?>.png"/>
</div>
</div>
<?php
}
?>
Edit
Remember, your file name is case sensitive. So the .png will need capital letters in their name. You can add the strtolower() around $company_name if you need them lowercase.
You have an comma at the end of this line which you don't need:
array('Arrowhead','http://www.arrowheadwater.com/'),
Please try to do basic debugging before posting.

Calling PHP function twice, only works once

I am trying to use PHP associative arrays to echo different values for text and images into HTML for different instances of a jQuery slideshow on the same page. Here's the HTML:
<div class='slide'>
<div class='mosaic-block fade'>
<div class='mosaic-overlay'>
<div class='text'><p>This is the text!</p></div>
</div>
<div class='mosaic-image'><img src='imgs/the-img.png'/></div>
</div> <!-- mosaic-block fade -->
</div> <!-- .slide --> `
I wrote arrays for each type of slideshow containing the text and image for each slide, here's and example:
$my_content = array(
'image1.png' => 'image1 text!',
'image2.png' => 'image2 text!'
);
Then I wrote a function with parameters for the category of slideshow and the content:
function gallery_content($content) {
foreach ( $content as $img => $txt ) {
echo "<div class='slide'>
<div class='mosaic-block fade'>
<div class='mosaic-overlay'>
<div class='text'><p>".$txt."</p></div></div>
<div class='mosaic-image'><img src='imgs/other/".$img."'/></div>
</div> <!-- mosaic-block fade -->
</div> <!-- .slide --> ";
}
I call it like this: gallery_content($my_content); and it works really well. But when I try to call it again for another set of values, only the first instance seems to work. I tried using the array directly instead of the variable as a parameter AND making a separate function for each slideshow, but keep getting the same results.
Why can't the function be called more than once? Thanks in advance.
My guess would be that PHP is doing its job. Check the source of the outputted document and you should see the proper number of galleries). I suspect that a CSS rule for the class gallery such as one that absolutely positions it is causing only one gallery to be visible. If you're OK with using inline CSS (which is not usually acceptable), you can have PHP add a custom position value (such as top) based on the gallery number:
function gallery_content($content) {
$num = 0;
foreach ( $content as $img => $txt ) {
echo "<div class='slide' style='top: ".(100 + 50*$num)."px'>
<div class='mosaic-block fade'>
<div class='mosaic-overlay'>
<div class='text'><p>".$txt."</p></div></div>
<div class='mosaic-image'><img src='imgs/other/".$img."'/></div>
</div> <!-- mosaic-block fade -->
</div> <!-- .slide --> ";
}
The example above gives the first gallery element a top of 100px (100 + 50 * 0), the second element a top of 150px, the tird 200px, and so on. You also could use some CSS3 and the new calc() feature in place of this, but CSS3 selectors are experimental and not supported in some older browsers. Using PHP and inline styles would be your safest bet.
See the source code that is generated on your final page(HTML) to judge whether PHP did its work or not. i think thats your resultant "galaries"(DIV) might be having the same ID or other attributes thorugh which the jQuery activates them, and so only one is being run properly (the first one), and the second one is not run.
Hope that helps.

Categories