bootstrap 4 grid mixed up when using margin - php

I am generating html code using php. I have some information stored in an array called $persons and I'm trying to generate a bootstrap card for each $person:
<div class="container">
<div class="row">
<?php foreach ($persons as $person): ?>
<div class="card col-md-3 m-1">
<img class="card-img-top" src="<?=person['img_src']?>" alt="<?=$person['name']?>">
<div class="card-block">
<h4 class="card-title"><?=$person['name']?></h4>
<p class="card-text"><?=$person['info']?></p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
Everything works fine when I remove m-1 class, but as soon as I add m-1 class, margin causes the last div to go to next line. I think lack of space causes this problem. How can I fix this problem? How to have margin between divs without the last div going to the next line?

You should have a separate div for the card since it's display:flex. Also, just use my-1 for margin-top and margin-bottom so that x-axis space is not effected...
<div class="container">
<div class="row">
<?php foreach ($persons as $person): ?>
<div class="col-md-3 my-1">
<div class="card">
<img class="card-img-top" src="<?=person['img_src']?>" alt="<?=$person['name']?>">
<div class="card-block">
<h4 class="card-title"><?=$person['name']?></h4>
<p class="card-text"><?=$person['info']?></p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
https://www.codeply.com/go/78AmkbWrLi

The easiest solution would be to just put another div inside the col, which applies the margin:
<div class="container">
<div class="row">
<?php foreach ($persons as $person): ?>
<div class="card col-md-3">
<div class="m-1"> <!-- NEW -->
<img class="card-img-top" src="<?=person['img_src']?>" alt="<?=$person['name']?>">
<div class="card-block">
<h4 class="card-title"><?=$person['name']?></h4>
<p class="card-text"><?=$person['info']?></p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
Or if you don't want to add another div, just add the m-1 class to the card-block.

Related

<a href=""> does not go to another page when pass value

I have a display_all_groups.php page that display a bootstrap card of all groups created by user. When users click on "View Group" hyperlink it will direct them to view_group.php
Here are my code
session_start();
require_once $_SERVER["DOCUMENT_ROOT"].'/mindspace/Business Services Layer/ManageGroupsController/GroupsController.php';
$std_id = $_SESSION['std_id'];
$group = new ManageGroupsController();
$data = $group->viewGroups($std_id);
?>
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-12 grid-margin stretch-card">
<form method="post">
<?php
foreach($data as $row){
?>
<div class="card-body">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="../../template/images/banner.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><?=$row['group_name']?></h5>
<p class="card-text"></p>
<a class="btn btn-primary" href="view_group.php?group_id=<?=$row['group_id']?>">View Group</a>
</div>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
</div>
</div>
When I try to click on the View Group button, it did not bring me to other page that I want it to. Please help, Im very new to PHP
It's hard to say exactly what the problem could be. First I would always check that $data is not empty. Then the keys are named correctly. I don't know the data object.
If everything is correct, then it should work. A small note: In the link there is still a simple quotation mark. You should delete that too.
<?= $row['group_id']?> >>>>**'**<<<< "
<?php session_start();
require_once $_SERVER["DOCUMENT_ROOT"] . '/mindspace/Business Services Layer/ManageGroupsController/GroupsController.php';
$std_id = $_SESSION['std_id'];
$group = new ManageGroupsController();
$data = $group->viewGroups($std_id);
?>
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-12 grid-margin stretch-card">
<form method="post">
<?php
foreach ($data as $row) {
?>
<div class="card-body">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="../../template/images/banner.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><? echo $row['group_name']; ?></h5> <!-- Used echo -->
<p class="card-text"></p>
<a class="btn btn-primary" href="view_group.php?group_id=<? echo $row['group_id']; ?>">View Group</a> <!-- used echo, removed extra single quote -->
</div>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
</div>
</div>
I had done a few changes in the code,
Used echo
Removed an additional single quote

How to remove unwanted break in bootstrap panel

I have panel where I'm displaying some basic informations about user. I want to display graph in the center of this panel.
I have problems with one unwanted space which I want to remove but I don't know how to do it.
Please look at image:
Code for this panel:
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">Detail zákazníka </div>
<div class="panel-body">
<div class="form-group">
<label>ID zákazníka:</label>
<p><?php echo !empty($zakaznici['ID_zakaznici'])?$zakaznici['ID_zakaznici']:''; ?></p>
</div>
<div class="form-group">
<label>Meno:</label>
<p><?php echo !empty($zakaznici['meno'])?$zakaznici['meno']:''; ?></p>
</div>
<div class="panel-body" align="center">Graph will be displayed here</div>
<div class="form-group">
<label>Priezvisko:</label>
<p><?php echo !empty($zakaznici['priezvisko'])?$zakaznici['priezvisko']:''; ?></p>
</div>
<div class="form-group">
<label>Email:</label>
<p><?php echo !empty($zakaznici['email'])?$zakaznici['email']:''; ?></p>
</div>
<div class="form-group">
<label>Adresa:</label>
<p><?php echo !empty($zakaznici['adresa'])?$zakaznici['adresa']:''; ?></p>
</div>
<a class="btn btn-primary" href="<?php echo site_url('Zakaznici/viewusersvypozicky/'.$zakaznici['ID_zakaznici']); ?>" role="button">Zobraziť prenájmy</a>
</div>
</div>
</div>
p tags create line breaks. You could replace the p tag with a span tag to remove the unwanted break.
Your div with the class name 'panel-body' is also occupying space. You could add position: absolute; to its defined styles and it would not create that break.
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">Detail zákazníka </div>
<div class="panel-body">
<div class="form-group">
<label>ID zákazníka:</label>
<p><?php echo !empty($zakaznici['ID_zakaznici'])?$zakaznici['ID_zakaznici']:''; ?></p>
</div>
<div class="form-group">
<label>Meno:</label>
<span><?php echo !empty($zakaznici['meno'])?$zakaznici['meno']:''; ?></span>
</div>
<div class="panel-body" align="center">Graph will be displayed here</div>
<div class="form-group">
<label>Priezvisko:</label>
<p><?php echo !empty($zakaznici['priezvisko'])?$zakaznici['priezvisko']:''; ?></p>
</div>
<div class="form-group">
<label>Email:</label>
<p><?php echo !empty($zakaznici['email'])?$zakaznici['email']:''; ?></p>
</div>
<div class="form-group">
<label>Adresa:</label>
<p><?php echo !empty($zakaznici['adresa'])?$zakaznici['adresa']:''; ?></p>
</div>
<a class="btn btn-primary" href="<?php echo site_url('Zakaznici/viewusersvypozicky/'.$zakaznici['ID_zakaznici']); ?>" role="button">Zobraziť prenájmy</a>
</div>
</div>
</div>
Try this
<div class = "row">
<div class = "panel panel-default col-4">
<div class = "panel-body">
//your matter
</div>
</div>
<div class = "panel panel-default col-4">
<div class = "panel-body">
//your graph
</div>
</div>
</div>
As discussed the p's are block level elements and create linebreaks - but Bootstrap also stlyes them with a 10px margin bottom so you may want to overwrite that with
p { margin-bottom: 0;}
I do not recommend this - the ideaa of the margin bottom is to separate text elements.
What will be causing your space issue where you have indicated is that you are using a div with a .panel body class within the panel body.
<div class="panel-body" align="center">Graph will be displayed here</div>
Bootstrap styles the panel body with 15px padding - so either
a) replace that class with a different class (this is what I would do - it makes no sense to have a .panel-body -within a .panel-body)
b) remove the padding from the nested panel body
.panel-body > .panel-body { padding: 0}
But as I said - a panel body within another one is semantically wrong.

Why do my images get positioned below each other when I set all of them to col-6?

I don't know why my images get positioned below each other even though they are inside a col-6 Bootstrap div. I want them to be next to each other, not below.
Code, in case it's unreadable in the text below: https://imgur.com/S6tSXng
Page: https://imgur.com/7FZl2lM
I have already tried putting the col-6 inside the foreach block, but it only made the second image smaller. I don't know what else to try.
<section id="listaoldal">
<div class="container">
<div class="row">
<div class="col-6">
<?php foreach ($characterDetails as $key):?>
<img data-toggle="modal" data-target="#<?= $key['modal']?>" class="img-fluid mx-auto d-block my-5 ddimages"src="characters/<?=$key['link'] ?>">
<?php endforeach;?>
Well, I expect the images to be next to each other like they should be not below.
foreach out of col-6.
<?php foreach ($characterDetails as $key):?>
<div class="col-6"><img ... /></div>
<?=endforeach?>
<section id="listaoldal">
<div class="container">
<div class="row">
<?php foreach ($characterDetails as $key):?>
<div class="col-6">
<img data-toggle="modal" data-target="#<?= $key['modal']?>" class="img-fluid mx-auto d-block my-5 ddimages"src="characters/<?=$key['link'] ?>">
<?php endforeach;?>
Actually your images are placed into one "col-6" you have to create one for each images, like this:
<div class="col-6">
<img .../>
</div>
<div class="col-6">
<img .../>
</div>
<div class="col-6">
<img .../>
</div>
...
Quick exemple of what you could use to make it work :
<section id="listaoldal">
<div class="container">
<div class="row">
<?php foreach ($characterDetails as $key):?>
<div class="col-6">
<img data-toggle="modal" data-target="#<?= $key['modal']?>" class="img-fluid mx-auto d-block my-5 ddimages"src="characters/<?=$key['link'] ?>">
</div>
<?php endforeach;?>
I recommend you to read more about Bootstrap's Grid system:
https://getbootstrap.com/docs/4.0/layout/grid/

Move the widget to the right side of the web page using bootstrap 4

I want the information tag to be at the right side of the web page. Rest of the things are in the middle of the page. While I am not able to move the information to the right side. Also, because of it the footer disappeared. While I remove the information code, the footer works. Please help me right-align the information widget and also have the footer too.
<div class="container">
<div class="card border-light mb-3 text-center">
<p class="card-header">Videos</p>
</div>
<div id="userSuccess" class="hide" role="alert">
<div id="successUserContent"></div>
</div>
<div class="row" id="userVid">
<?php
$allVid = Schema::getAll();
for ($i = 0; $i < count($allVid); $i++) {
echo <<<HTML
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<div class="card-body">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<h4 class="card-title">{$allVid[$i]->getTitle()} </h4>
</div>
</div>
</div>
HTML;
}
?>
</div>
<div class="row">
<div class="col-md-4 order-md-2 mb-4">
<div class="card my-4">
<h5 class="card-header">Information</h5>
<div class="card-body">
<div class="row">
<ul class="list-unstyled mb-0">
<li>
...
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
require_once './page/footer.php';
?>
For right align, you'll want to add the class of "justify-content-end" after your "row" class.
<div class="row justify-content-end"><!-- ADDED HERE-->
<div class="col-md-4 order-md-2 mb-4">
<div class="card my-4">
<h5 class="card-header">Information</h5>
<!-- REST OF YOUR CODE-->
For your footer, you'll need to wrap your URL in parenthesis.
<?php require_once('/yourdirectory/yourfooter.php'); ?>

Splitting article on multiple images

I got an article that is read from the database. Which is in a wrapper, I needed an image to be full width (so even wider than the wrapper) while still being in the article. So I split it on the image tag like the code below.
However this prevents a user from adding multiple images. When I add another image tag it doesn't show the image.
How can I split the article not just on one image but every image?
All data (text, img tags) is in 'introtext'.
For example:
<p>Part one of the article</p>
//Here the article is split on all img tags that are there.
<img src="#">
<img src="#">
<img src="#">
<img src="#">
<p>Part two of the article</p>
The below code only works when I add one image tag.
<div class="container relative" style="padding: 0px;">
<div class="row">
<div class="col-sm-12">
<div class="blog-item mb-20 mb-xs-40">
<!-- Text -->
<div class="blog-item-body">
<?
$introtext = $contenti[0]['introtext'];
$splitartikel = preg_split('/(<img[^>]+\>)/i', $introtext, -1, PREG_SPLIT_DELIM_CAPTURE);
echo $splitartikel[0];
?>
</div>
</div>
</div>
</div>
</div>
<div class="container-fluid relative" style="padding: 0px;">
<div class="row">
<div class="col-sm-12">
<div class="middleimage">
<?
echo $splitartikel[1];
?>
</div>
</div>
</div>
</div>
<div class="container relative" style="padding: 0px;">
<div class="row">
<div class="col-sm-12">
<div class="blog-item mb-20 mb-xs-40">
<!-- Text -->
<div class="blog-item-body">
<?
echo $splitartikel[2];
?>
</div>
</div>
</div>
</div>
</div>
The query:
// Articles
$content = "SELECT * FROM `lb_content` WHERE alias = '".$conn->real_escape_string($_GET['alias'])."' ";
$contentcon = $conn->query($content);
$contenti = array();
while ($contenti[] = $contentcon->fetch_array());
I also tried looping the middle part, but this also loops the text before and after the image, while I only want to have the option to have multiple images.
Like this:
<?php foreach($splitartikel as $part) : ?>
<div class="container-fluid relative" style="padding: 0px;">
<div class="row">
<div class="col-sm-12">
<div class="middleimage">
<?php echo $part; ?>
</div>
</div>
</div>
</div>
<?php endforeach ?>
Maybe I need to do some sort of foreach loop for every image tag?

Categories