Weird Spaces After PHP Generated HTML - php

I have a weird issue. I have a dynamic website www.lamanscanada.com it appears fine in firefox and safari and almost perfect in IE7. But it appears that on certain php generated html. There is a bunch of space added after the function output. I also user mod_rewrite for nice SEO urls just a note.
The function sorts an array of data into columns with bullets
<?php
//breaks array values into two columns
function groupBullets($array){
//count the array items
$bulletcount = count($array);
//divides that in two and round the result
$roundhalf = round(($bulletcount/2));
//initailize a counting variable
$i=1;
//loop throught the array
foreach($array as $bullet){
//if it is the first item
if($i==1){
echo "<span class=\"container\">";//open the main container div
}
echo "<span class=\"bullet\">• $bullet</span>";//then create bullet
//if the counter is at the half mark
if($i==$roundhalf){
echo "</span>";//close the container
$i=1;//and start the counter over
}else{
$i++;//if not at half keep counting
}
}
}
//end
//I am implementing it like this:
//this is my array it actually has a second level
$array = array(
columngroup01("bullet 1","bullet 2","bullet 3","bullet 4"),
columngroup02("bullet 1","bullet 2","bullet 3","bullet 4")
);
//I loop through this array
foreach($array as $column=>$arrayofbullets){
echo "<div class=\"columncontainer\">".//open the div contains the columns
"<div class=\"heading\">$column</div>";//print the title of the column
//use the groupBullets function on each array of bullets
groupBullets($arrayofbullets);
echo "</div>";//close the column container
}
?>

The <span class="features"> is not being closed at the end of the generated list of <span class="featureitem"> tags.
Current generated output:
<div class="infoblock">
<div class="infoheading">features</div>
<span class="features">
<span class="featureitem">• 10'X6'X8' Aluminum Extrusion Structure</span>
<span class="featureitem">• R18 Insulated Panels</span>
...
</div>
Needs to be:
<div class="infoblock">
<div class="infoheading">features</div>
<span class="features">
<span class="featureitem">• 10'X6'X8' Aluminum Extrusion Structure</span>
<span class="featureitem">• R18 Insulated Panels</span>
...
</span>
</div>

Related

Sort Gallery by value of specific Array Key, then display value of another key [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 months ago.
I have a multidimensional array that is used to pull images and data into the page. The page will also have a sort by system which will display only the array items that have a certain tag (currently tagging yes or no with 0 and 1) I am using an if statement to decide whether the page displays the default (all of the photos) or the sorted array. The default works fine on its own, but im trying to get the sorted to display. When i include the html/php for displaying the images, instead of "echo $mImage['img_caption'];" (Which is a stand in, im just testing if i can even get the statement to display the items. Instead of the full caption string, it displays the first character of every value of every key.) i get errors akin to "unexpected "{" or "<" and the page wont load. Ive basically had to teach myself php over the course of three days, so im sure im missing something easy. Ill include an example of the array, the php for the sort, and the section that works to display the entire array.
Here is my arrays
$arrImages[] =
[
'img_sm'=>'exampleple.jpg',
'img_lg'=>'example2.thumb.jpg',
'img_caption'=>'multifamily',
'img_description'=>'example',
'img_path' => 'img/getest',
'img_type'=>'getest',
'MultiFamily'=>1,
];
$arrImages[] =
[
'img_sm'=>'example.jpg',
'img_lg'=>'example.thumb.jpg',
'img_caption'=>'notmultifamily',
'img_description'=>'example786',
'img_path' => 'img/getest',
'img_type'=>'getest',
'MultiFamily'=> 0,
];
And here is the if statement
<?php
$sorted = 1;
//If the viewer is sorting, check what they are sorting by and then sort by that before pushing the page to change display.
if ($sorted = 1){//start if sorted statement
$MultiFamilySorted = 1; //replace this with a function that checks all checkboxes to see if they are sorted by that or not. This is simulating that the viewer is sorting to see only multifamily homes.
foreach($arrImages as $sortedImages)://Start sort loop
if ($sortedImages['MultiFamily'] = 1){//start display sorted
foreach($sortedImages as $mImage): //start display loop
echo $mImage['img_caption'];//i am just using this to test if it will display
endforeach;
}//end display sorted
endforeach;
}//end if sorted statement
?>
This is the section for my default display
<div class="ImageDisplayTestBox">
<?php
foreach($arrImages as $mImage): //loop through the image array
?>
<div class="masonry-item no-default-style col-sm-3">
<a href="<?php echo $mImage['img_path'] . '/' . $mImage['img_lg']; ?>">
<span class="thumb-info thumb-info-centered-info thumb-info-no-borders">
<span class="thumb-info-wrapper">
<img src="<?php echo $mImage['img_path'] . '/' . $mImage['img_sm']; ?>" class="img-fluid" alt="">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $mImage['img_caption']; ?></span>
<span class="thumb-info-type"><?php echo $mImage['img_type']; ?></span>
</span>
<span class="thumb-info-action">
<span class="thumb-info-action-icon"><i class="fas fa-plus"></i></span>
</span>
</span>
</span>
</a>
</div>
<?php endforeach; //end loop ?>
</div>
Thanks!
I did not check you code for the syntax error you mentioned, but there is a simpler way of doing this:
<?php
if ($sorted == 1){
$arrImages = array_filter($arrImages, function($image){
return $image['MultiFamily'] == 0;
});
}
?>
You can use this instead of your sorting code.
This executes the sorting function for each array member. If the function returns true it will pass it to the the sorted array. If it is false it will skip it.
The documentation of array_filter is here: https://www.php.net/manual/de/function.array-filter.php

PHP For Loop Count in While Statement

I'm trying to figure out the best solution on how to output my html with the php while counting up on a div class.
This is my function that prints news:
function post_news()
{
$post_news = $this->mysql->Query("SELECT * FROM news ORDER BY id DESC LIMIT 4");
while ($news_row = mysqli_fetch_array($post_news))
{
echo "<div class='column1'>
<div class='box'>
<h3>".$news_row['title']."</h3>
<p>".$news_row['content']."</p>
<a href='index.php?p=news&id=".$news_row['id']."' class='button button-small'>Read More</a> </div>
</div>";
}
}
What I am attempting is, where it shows <div class='column1'> I need it to count upward to class='column4'. When I attempt For loops or a second While loop I can sometimes get it to count but it always does a new line where I want these blocks to display side by side like normal.
So what is the best way to count up on my div class without it doing a line break after every block?
I assume that when you say 1 to 4 you want it to start over at 1 after 4. Just increment a counter and reset it when it reaches 4:
$num = 0;
while ($news_row = mysqli_fetch_array($post_news))
{
$num++;
echo "<div class='column{$num}'>
<div class='box'>
<h3>".$news_row['title']."</h3>
<p>".$news_row['content']."</p>
<a href='index.php?p=news&id=".$news_row['id']."' class='button button-small'>Read More</a> </div>
</div>";
if($num == 4) { $num = 0; }
}
Or actually, you use LIMIT 4 in the query so you don't really need the if statement that resets it to 0, as the loop will only iterate 4 times.
A pure html workaround to this would be to have the code like so:
<div class='column1'>
...
</div><div class='column2'>
..
</div>
But because you're echoing it that's not going to work. Another thing you can do is specify an inline-block css property for those columns. If you're using bootstrap you can just use col-xs-3 so that you can have 4 even columns.
If not, instead you're going to have to specify each of those classes to be width:25%
So to wrap up, in css:
.col{
width:25%;/*probably going to have make responsive for screen width*/
display:inline-block;/*or float:left, whichever*/
}
And in php/html:
...
echo "<div class='column1 col'>
<div class='box'>
<h3>".$news_row['title']."</h3>
<p>".$news_row['content']."</p>
<a href='index.php?p=news&id=".$news_row['id']."' class='button button-small'>Read More</a> </div>
</div>";
...
Hope that helps.

Access a special div, generated by a while loop

I'll try to be clear. My problem is that i'm using php to get data from mysql. The table has more than one record. To show table records i'm using a while loop that as a condition has mysqli_fetch_array(). The records from the table must be shown in an echo(because i want to show them as html on the page), but every div generated from the while loop has a link to send me to another page, and if the client clicks on that link it will get the current divs information(after the table in database has more than one record the informations will be different) to be shown at the next page. Hope you understood it. Thanks in advance!
while($rows1 = mysqli_fetch_array($query1,MYSQLI_ASSOC))
{
echo "<div class=\"row\">
<div class=\"col-md-6\">
<div class=\"thumb\">
<figure>
<img src=\"images/extra-images/room-grid1.jpg\" alt=\"\"/>
<figcaption>
<a rel=\"prettyPhoto[gallery2]\" href=\"images/extra-images/room-grid1.jpg\">
<i class=\"fa fa-search\"></i>
</a>
</figcaption>
</figure>
</div>
</div>
<div class=\"col-md-6\">
<div class=\"text\">
<h4>".$rows1['Dh_lloji']."</h4>
<p>".$rows1['Dh_Pershkrimi']."</p>
<ul class=\"room-grid-meta\">
<li>Max: ".$rows1['Dh_Kapaciteti']."</li>
<li>Size: ".$rows1['Dh_madhesia']."</li>
<li>Floor: ".$rows1['Dh_Kati']."</li>
</ul>
<div class=\"retail room-grid-retail\">
<span>
<sup>$</sup>
".$rows1['Dh_cmimi']."
<sub>night</sub>
</span>";
if(isset($_SESSION['Emri_Mbiemri']) != '')
{
echo "<a class=\"btn-3\" href=\"payment.php\">Book now</a>";
}else{
$error = "You must be logged in to book!";
}
echo "<br>".$error."</div>
<div id=\"price_room\">".$rows1['Dh_cmimi']."</div>
</div>
</div>
</div>";
$count++;
}
Looks like you need at least your custom counter, that will be increment inside cycle, and set div's id based on this counter plus use it in generated link
I made a solution for my problem and i wanted to share it with you guys. I gave a $count to the links defining every link the while loop generates...
<a class=\"btn-3\" href=\"payment.php?id=$count\">Book now</a>
After that i used a multidimensional array to store the current divs variable(in my case rooms price, the count, and the id) of the room from database...
$room_prices[$count][0] = $rows1['Dh_cmimi'];
$room_prices[$count][1] = $count;
$room_prices[$count][2] = $rows1['ID_Dhoma'];
After the client click a specific link he will get the id so i can make a search at my multidimensional array for the price and the id of that room..
for($i = 0; $i < $count; $i++)
{
if($_GET['id'] == $room_prices[$i][1]){
$sess_room_price = $room_prices[$i][0];
$sess_room_id = $room_prices[$i][2];
}
}
After i get these i store them at the global session variables to use them at the other page...
$_SESSION['Rooms_Price'] = $sess_room_price;
$_SESSION['Rooms_ID'] = $sess_room_id;
So this did the work for me... Hope this helps others.

Automatic generation of <div> tag into Html webpage

i have these lines of code:
<div class="signals">
<ul>
<li>First Signal</li>
<li>Second Signal</li>
<li>Third Signal</li>
</ul>
</div>
<div id="signal1" style="display:none;">
<p style="color:#fff">First comment for #signal1 id - it will open in a fancybox -.</p>
<div id="signal2" style="display:none;">
<p style="color:#fff">Second comment for #signal2 id - it will open in a fancybox -.</p>
</div>
<div id="signal3" style="display:none;">
<p style="color:#fff">Third comment for #signal3 id - it will open in a fancybox -.</p>
</div>
Here it is the jsfiddle code: JsFiddle
Right now when i want to show different comments, i open my html file and edit the "id #signal , adding more id (or deleting them) when i need it.
The problems is: the signals can be more than the three that are showed up, or even less.
So my question is:
There is a way to generate automatically the divs that i need in a second sheet, where i will insert the comment and all the id's? (a sort of backend)
For example: if one day i need just 2 signals, i will create the #signal1 and #signal2 div, i'll insert the comments and save the secondary sheet.
When i do that, the primary sheet with the html stuff, will show 2 "li" lines:
First Signal
Second Signal
and when i click on of them, the fancybox will open and show the comment, just as the code into the jsfiddle.
I don't know much of php, but there is a way i can do that using it? Or there is a better way?
Hope i can learn from your help.
It seems that you need PHP foreach loop.
You can write the comments in a PHP array, and read them using PHP foreach.
Here is the sample code:
<?php
$commentsLabels = array('First Signal','Second Signal');
$commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');
//You could use a 2D array to store both comments Labels and comments Texts in one array, but since you are new to PHP, I put them in 2 different arrays to make it easy.
//You can add more comments texts/labels to the arrays in double/single quotes separated with ','
//For now, the arrays are filled manually. in future, you can add a DB to store comments and fetch the values from there.
$counter = 0;
//we need a counter to make the comment ids dynamic
?>
<!--GENERATING LABELS-->
<div class="signals">
<ul>
<?php
foreach ($commentsLabels as $commentLabel) { //loop starts
$counter++; //increasing $counter by 1, for each iteration
//We have to add HTML tags outside the php code block.
?>
<li><?php echo $commentLabel; ?></li>
<?php
} //loop ends
?>
</ul>
</div>
<!--GENERATING POPUPS-->
<?php
$counter = 0; //reset counter
foreach ($commentsTexts as $commentText) { //loop starts
$counter++;
?>
<div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php
} //loop ends
?>
I hope this helps you start. :-)

How to show 3 random divs on 3 random places

How to show 3 random divs on 3 random places
I have 6 potentionaly places for divs.
All 6 divs need to be hidden for now.
When page load I need to show only 3 random divs, and another 3 divs need to be hidden.
<?php
$divs = array('<div id="divFirst">First Div</div>','<div id="divFirst">Second Div</div>','<div id="divFirst">Third Div</div>');
// Array with 3 random keys from $divs
$randKeys = array_rand($divs, 2);
//echo $divs[$randKeys[0]]; // First random div
//echo $divs[$randKeys[1]]; // Second random div
//echo $divs[$randKeys[2]]; // 3rd div
//shuffle($divs);
?>
<div class="text">This is a simple text</div>
<?php echo $divs[$randKeys[0]]; ?>
<div class="text">This is a simple text</div>
<?php echo $divs[$randKeys[1]]; ?>
<div class="text">This is a simple text</div>
<?php echo $divs[$randKeys[2]]; ?>
<div class="text">This is a simple text</div>
<?php echo $divs[$randKeys[1]]; ?>
<div class="text">This is a simple text</div>
<?php echo $divs[$randKeys[0]]; ?>
<div class="text">This is a simple text</div>
<?php echo $divs[$randKeys[2]]; ?>
Look at this: $randKeys = array_rand($divs, 2);
The second parameter specifies the number of items you want out of the array. You want 3, but you told it you want 2.
Then you're doing <?php echo $divs[$randKeys[2]]; ?> That is, trying to print the third element from the array (i.e. arrays start counting at 0). But you only have 2 elements in the array now.
Also, it would make more sense to only keep the contents of the divs in your array. Then you can do like:
$randKeys = array_rand($divs, 6);
//because you want 3 shown, 3 hidden, thus 6
for($i=0; $i<6; $i++)
{
$style='display: block';
if($i>=3)
{
//we hide the last 3
$style='display: none';
}
echo "<div id='div_$i' style='$style'>" . $divs[$randKeys[$i]] . "</div>";
}
That solves the problem mentioned in the comments about your div ids not being unique.
However, in order to pick 6, you need to actually have 6 elements in the $divs array because the docs for array_rand() state:
Trying to pick more elements than there are in the array will result in an E_WARNING level error, and NULL will be returned.

Categories