Calling PHP function twice, only works once - php

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.

Related

giving a CSS class as part of a PHP if else statement

Is there a good way of calling a css class as part of a PHP if else statement? What I am trying to do is change the background color of a div that is a part of the html depending on the value of the results from a php if else statement.
php
if(!empty($check_availability)){
echo <div id="1"> do something <div>
// make the div background green if this is true
}
else {
<div id="2"> do something else instead<div>
//make backgound red if this is true
}
html
<div id="container"> <!-- <<<<<<< this is the div that I want to change the background color of depending on results from the php -->
<div id="1"></div>
<div id="2"></div>
</div>
I assuming you are talking about conditionally adding classes to divs. good is subjective, but this is what I normally do.
$class = !empty($check_availability) ? 'green-class' : 'red-class';
echo "<div class='{$class}'> do something <div>";

Checking biggest heigh from dynamic generated divs

I have a structure of divs that are generated based on php code. Each row has 3 divs. It is generated like this:
Defining columns:
<?php
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
First row, starting the loop and defining width:
<div class="row">
#foreach($datas as $data)
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
Then there's a bunch of content.
Then, in the end, each loop adds one to rowcount, and checks if the row has ended, so we can add a new one:
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
?>
#endforeach
Now, it all works great, the problem is that as I add different amounts of content on each div, the heights become different and ends up becoming quite ugly.
What I'm trying to do: I'm trying to check in each row which div has the biggest height, so I can apply the same height to the other two.
-I've thought of adding a id to each div that increments, problem is I don't know how many divs there will be, so I can't prepare for each outcome.
-I've tried selecting each row with $('row div'), problem is then all divs will become the same size as the biggest of all. And I want to check by row.
Sorry for the long question, and thanks for any input!
Bootstrap 4 has flexbox classes that allow you to do this with just css. Take a look at this example: https://getbootstrap.com/docs/4.3/utilities/flex/#align-self
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex">
<div class="align-self-stretch mr-3" style="background-color:lightblue">Aligned flex item</div>
<div class="align-self-stretch mr-3" style="background-color:lightblue">Aligned flex item<br />With<br />More<br />Lines</div>
<div class="align-self-stretch" style="background-color:lightblue">Aligned flex item</div>
</div>
I think flex-box is your solution : all into flex container will have the same height.
<div style="display:flex">
<div>
... some content
</div>
<div>
... some content
</div>
</div>
You can use .row-eq-height in Bootstrap 4

PHP Code Within Element Causes Repetition

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.

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.

Hiding a div with the same class as another using jQuery?

Basically, I would like to accomplish the following with jQuery:
<div class="120 most-voted">
<!-- DON'T HIDE THIS DIV -->
</div>
<div class="110 voted">
<!-- some stuff here -->
</div>
<div class="120 voted">
<!-- hide this div with class '120' since there's already
another div with class '120' at the top -->
</div>
<div class="23 voted">
<!-- some stuff here -->
</div>
EDIT: The numbers are dynamically generated by a PHP function:
<?php $postid = get_the_ID(); // capture the id ?>
<div id="<?php echo $postid; ?>" class="most-voted">
I don't want to hide the div at the top.
Any suggestions (I don't mind wrapping another div in the div at the top to accomplish this result)?
This will make the first div with class "v120" visible and all others hidden:
var theClass = "v120";
$("div." + theClass).first().show().end().not(":first").hide();
Live example
(I added the "v" to "120" because I wasn't sure whether "120" is a valid CSS class.)
How that works:
Find all divs with the class - $().
Reduce that set temporarily to the first match - .first()
show that one (in case it used to be hidden)
End the temporary reduction of the set - .end()
Reduce the set to only the matches that aren't the first - .not(":first")
hide them
Its not valid html, the id must be unique within the document
http://www.w3.org/TR/html401/struct/global.html#h-7.5.2
Use rel attributes instead of IDs. Then use selector like:
$('div.voted[rel=110]')
If you want to use same div, you should conceder using a class attribute since double id is not allowed.
<div class="most-voted id_120">
<!-- some stuff here -->
</div>
<div class="voted id_110">
<!-- some stuff here -->
</div>
<div class="voted id_120">
<!-- hide this div with id 120 since its already
at the top in the div with class most-voted -->
</div>
<div class="voted id_100">
<!-- some stuff here -->
</div>
use this to locate the first on http://api.jquery.com/first-selector/

Categories