Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I need help. Basically I am trying to make a blog and I want to display some posts on main page but I dont know how to limit number of characters from content.
Here is my code:
<?php foreach($query as $q){ ?>
<div class="card" style="margin: 5px; width: 18rem;">
<div class="card-body">
<h5 class="card-title"><?php echo $q['title'];?></h5>
<p class="card-text"><?php echo $q['content'];?></p>
Read more...
</div>
</div>
<?php }?>
I tried this:
<?php foreach($query as $q){ ?>
<div class="card" style="margin: 5px; width: 18rem;">
<div class="card-body">
<h5 class="card-title"><?php echo $q['title'];?></h5>
<p class="card-text"><?php substr(echo $q['content'];,0, 50)?></p>
Čítaj ďalej...
</div>
</div>
<?php }?>
and much more solutions but it did not help.
Some help please?
I believe you want to set a max number of characters for some some text.
Change the style to select the number of lines you want to show. The -webkit-line-clamp: 4; will be responsible to tell the max number of lines you want to have:
<style>
.card-text{
width:100px;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 4;
-webkit-box-orient: vertical;
}
</style>
If you want the content in only one line:
<style>
.card-text{
display: inline-block;
max-width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
But if the goal is really to hide 100% the content from the user using your backend, the previous methods shown by the others using php substrig is enough.
You can do this:
$string = "Oi, eu sou o matheus. Texto adicional";
echo substr($string, 0, 2); // outputs Oi
echo substr($string, 4, 16); // eu sou o matheus
substr(echo $q['content'];,0, 50); is not going to output the data you want . (substr is a PHP function to get a part of a string say from position x and length y with the syntax substr($string, x, y))
Please change
<?php substr(echo $q['content'];,0, 50)?>
to
<?php echo substr($q['content'],0,50); ?>
Errors
The syntactic error when you added ; inside of the function.
Putting echo which returns void where substr expects a string.
Correction
<?= substr($q['content'],0, 50) ?>
Recommendation
You can shorten you PHP echo inside of the HTML templates using the short echo tag
<?= $a ?>
Which is equivelent to
<?php echo $a; ?>
Example
<?php
$content = "
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and Ipsum.
";
?>
<?= substr($content,0, 50) ?>
Test https://3v4l.org/HHbZQ
Here you go and if var not exists echo something random with ??
<?php
echo substr(($q['title'] ?? 'sometext'), 0, 30);
?>
And with a function to check or something else
<?php
echo substr(MYFUNCTION($q['title'] ?? 'sometext'), 0, 50);
?>
Related
Im making a form using PHP/HTML.
I have divided all my fields into divs, and i get all my values from a table from SQL server.
I want to make some divs disappear if the value im getting from the table is empty.
I know how to do it, but it just does not work if the variable is empty.
For example;
<div <?php if (empty($trabalhoSemCarteira)){?>style="display:none"<?php }?>>
<h2>Trabalho sem registro em carteira:</h2>
<div>
<label><?php echo $trabalhoSemCarteira;?></label>
</div>
</div>
This does not work,and i have no idea why.
On the other hand;
<div <?php if ($numeroFilhos<3){?>style="display:none"<?php }?>>
<label>Nome do Filho: <?php echo $nomeFilho3;?></label>
<label>Idade: <?php echo $idadeFilho3;?></label><br>
</div>
This one works like a charm. I'm doing exactly the same thing in both of them. I think the problem is with the ''empty'' function, I don't know, but that is literally the only difference.
EDIT:I have tried using ==null,is_null() instead of empty and nothing works.
I'm not super well versed with your database collection code, so I can't say with absolute certainty what the problem is. First off, as CBroe mentioned, use var_dump($trabalhoSemCarteira) to find what your variable actually is when you get nothing back. Then just key it into the if statement. Example:
<?php if($trabalhoSemCarteira === $a){?>style="display:none;"<?php }?>
Of course, $a is meant to be whatever var_dump($trabalhoSemCarteira) outputs.
Note: Please make you code cleaner. I actually couldn't really tell at first if the format would even work. Unless you're using the empty blocks for something else(such as displaying the blocks later with JS, in which case your approach makes absolute sense), to prevent you rendering unnecessary blocks of useless code, only render the block if the variable does exist. Example:
<?php if($trabalhoSemCarteira !== $a): ?>
<div>
<h2>Trabalho sem registro em carteira:</h2>
<div>
<label><?php echo $trabalhoSemCarteira;?></label>
</div>
</div>
<?php endif; ?>
Obviously, again, $a is whatever your var_dump($trabalhoSemCarteira) output.
Edit: I just read the notes and you say that it gives you an empty space. Replace $a in the examples with .
Make my div disappear if variable is empty?
You can approach this a different way and instead of using a conditional in PHP, you can use the
:empty
pseudo-class in CSS which applies styles to HTML elements which don't contain anything.
Working Example:
.outer-div {
display: inline-block;
margin-right: 12px;
width: 120px;
height: 120px;
background-color: rgb(255, 0, 0);
vertical-align: top;
}
.inner-div {
display: flex;
justify-content: center;
align-items: center;
width: 96px;
height: 96px;
margin: 12px;
text-align: center;
background-color: rgb(255, 255, 0);
}
.inner-div:empty {
opacity: 0;
}
<div class="outer-div">
<div class="inner-div">PHP String Variable Echoed Here</div>
</div>
<div class="outer-div">
<div class="inner-div">A</div>
</div>
<div class="outer-div">
<div class="inner-div"></div>
</div>
Further Reading:
https://developer.mozilla.org/en-US/docs/Web/CSS/:empty
Your value is not empty it contains a single blank space, the easiest way to check is by trimming the value before checking
<?php $trabalhoSemCarteira=" "; ?>
<div <?php if (empty(trim($trabalhoSemCarteira))){?>style="display:none"<?php }?>>
<h2>Trabalho sem registro em carteira:</h2>
<div>
<label><?php echo $trabalhoSemCarteira;?></label>
</div>
</div>
BTW, TIP: indeed, as Ruvee mentioned syntax you're using for generating your HTML with PHP is just messy and it is error-pron, use something cleaner pls:
<?php
$trabalhoSemCarteira=" ";
$clazz = (empty(trim($trabalhoSemCarteira))) ? 'none' : 'block';
?>
<div style="display: <?php echo $clazz ?>">
<h2>Trabalho sem registro em carteira:</h2>
<div>
<label><?php echo $trabalhoSemCarteira;?></label>
</div>
</div>
or even cleaner
<?php
$trabalhoSemCarteira=" ";
$clazz = (empty(trim($trabalhoSemCarteira))) ? 'none' : 'block';
echo "<div style='display: {$clazz}'>
<h2>Trabalho sem registro em carteira:</h2>
<div>
<label>{$trabalhoSemCarteira}</label>
</div>
</div>"
TIP2: If you're gonna build some larger project in the feature consider using some MVC framework and/or some template engine like i.e. Twig, Smarty or others.
CSS
.number{
float:none;
background-color:white;
cursor:ponter;
}
#panel{
background-color:red;
height:200px;
width:100px;
overflow-y:scroll;
}
I want to make a list of number in a panel. I've tried with HTML
HTML
<div id="panel>
<span class="number">1</span>
<span class="number">2</span>
<span class="number">3</span>
<span class="number">4</span>
.....
<span class="number">50</span>
</div>
When <span> is clicked, something will appear by jQuery, but I have no problem with jQuery.
Because I thought that looping the number manually doesn't efficient, I tried to use PHP.
PHP
<?php
for($number=0;$number<=50;$number++){
echo "<span class='number'>".$number."</span>";
}
?>
But the number made by PHP doesnt do the same like HTML does.
This is what I want and done by HTML.
This is done with PHP and the numbers are made horizontally until 50
You need to make sure the same whitespace is present when looping through it in PHP:
<?php
for($number=0;$number<=50;$number++){
echo "<span class='number'>".$number."</span>\n";
}
?>
Remember, your original code is just outputting one long string:
<span class='number'>1</span><span class='number'>2</span>...
In this case, whitespace (A newline) is important which may alter how your CSS looks. Forcing a new line each time you echo out a <span> by adding \n should fix this.
.number{ display : inline-block; }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My image link has "invisible pixels" that show up as a link when you hover above the actual image. Is there any way to remove them? Here is my code:
<div id="sidebar">
<div id="navbuttonbox">
<img src="img/test.png"/>
</div>
</div><!--sidebar-->
For the style I use
#navbuttonbox {
margin-left: 37px;
}
So that It will be right where I want it.
I demonstrated it on JSFIDDLE
https://jsfiddle.net/1g2pqwy2/1/
When you move your mouse a little bit above the image you still get a cursor link because the tail of the image is higher then the body.
First here is your HTML.
<div id="sidebar">
<div id="navbuttonbox">
<img src="img/test.png">
</div>
</div>
Try this:
HTML
<div id="sidebar">
<div class="navbuttonbox">
Movies
</div>
<div class="navbuttonbox">
OTHER
</div> <!--- KEEP REPEATING FOR EACH MENU ITEM //-->
</div>
CSS
.navbuttonbox {
position:relative;
background: url(/img/test.png);
background-repeat: no-repeat;
min-height: 40px;
padding-top: 10px;
margin-right: -38px;
}
.navbuttonbox > a {
display:block;
padding:10px;
}
You'll notice the link is shifted down for those invisible pixels. The tail can be fixed too if you need it with:
.navbuttonbox > a:before {
content:'';
width:38px;
top:-10px;
bottom:0;
right:0;
position:absolute;
}
To demonstrate I set up a JSFiddle: here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
For some reason when I am trying to print a welcoming message when the user logs on my site and make it styled in CSS. Whenever I try to do it, it prints the text not styled and after it it leaves " ?>
What is wrong?
Here is the CSS code for the class box
#box
{
border: 10px solid #a1a1a1;
padding: 120px 40px;
width: 1163.5px;
border-radius: 25px;
}
<!---Submission Box--->
<div id="box">
<!---Welcoming Message--->
<?php
session_start();
echo "<div id="text">Hello, </div>"
?>
</div>
You need to either escape the quotes you use if you wrap them with the same kind of quotes, or use different quotes. You should also have a semicolon at the end of the echo.
If you use the double quotes, PHP will parse the string and replace the variable with it's value. If you use single quotes you'll need to concatenate the string.
echo "<div id=\"text\" style=\"color:red;\" class=\"my-class\">Hello, $name</div>";
echo '<div id=\'text\' style=\'color:red;\' class=\'my-class\'>Hello, ' . $name . '</div>';
echo "<div id='text' style='color:red;' class='my-class'>Hello, $name</div>";
echo '<div id="text" style="color:red;" class="my-class">Hello, ' . $name . '</div>';
You should also move session_start(); to the very top of your script before there is any output.
From your updated code, you still need to fix the quote issue. Place a semicolon at the end of the echo line. Move the session_start(); to the header. You're not echoing any variable on the line so it's not necessary to use PHP to print out the HTML.
#box
{
border: 10px solid #a1a1a1;
padding: 120px 40px;
width: 1163.5px;
border-radius: 25px;
}
<!---Submission Box--->
<div id="box">
<!---Welcoming Message--->
<?php
echo "<div id=\"text\">Hello, </div>";
?>
</div>
You need to escape your quotes and move your session_start() to the top of the page:
<?php
session_start();
?>
:
:
:
<!---Submission Box--->
<div id="box">
<!---Welcoming Message--->
<?php
echo "<div id=\"text\">Hello, </div>";
?>
</div>
First of all, you need to start the session before sending any output (before the page headers would be written):
<?php session_start(); ?>
<!---Submission Box--->
<div id="box">
<!---Welcoming Message--->
<?php echo "<div id=\"text\">Hello, </div>"; ?>
</div>
If you want to put " within " you have to escape it:
echo "<div id=\"text\">Hello, </div>";
As I understand you want to include a variable in the echo.
For example if you have the user's name in $name variable,
you can do this:
<?php echo "<div id=\"text\">Hello, $name</div>"; ?>
Your double quotes are destroying your php!
You could use single quotes, and you are missing a semicolon:
<!---Submission Box--->
<div id="box">
<!---Welcoming Message--->
<?php
session_start();
echo "<div id='text'>Hello, </div>";
?>
</div>
Use single quotes or use back slash on the id quotes. Your choice. Also add in the ; at the end
Method 1, single quotes
echo '<div id="text">Hello, </div>';
Method 2, back slash:
echo "<div id=\"text\">Hello, </div>";
My choice single quotes:
<!---Submission Box--->
<div id="box">
<!---Welcoming Message--->
<?php
session_start();
echo '<div id="text">Hello, </div>';
?>
</div>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've looked high and low and cannot find it. Here are Lines 48-55
//add the cover page sales link here
echo "<h2 class='posttitle'>
<a href='http://divorcemagazinecanada.com/buy-divorce-magazine-online'>Get Your Divorce Magazine By Mail - Click Here</a>
</h2><a href='http://divorcemagazinecanada.com/buy-divorce-magazine-online'>
<img src='http://divorcemagazinecanada.com/wp-content/uploads/2014/07/Divorce.mag_.cover_.jpg'; width='100%'; height='100%';></a><br><br>
<div id="future" style='width:100%; height:auto;'>
<div id="turnery" style="width:280px; height:auto; border:1px solid #fff; float:left; padding:5px; position:relative;">
<div class="featuredpost">
The error is caused by an unescaped quote in a quoted string. This is just a messy situation all around - such strings rarely belong in code.
I would move the HTML out of PHP (strings) and any required value interpolation into the HTML - note the use of the <?= ?> tags as examples. This let's PHP function more as a template engine and does a tiny little bit to separate the concerns.. and there are template engines to clean this up even more.
<?php /* some php stuff way up here */ ?>
<!-- plain markup, outside of any PHP block -->
<h2 class='posttitle'>
<a href='<?= $theUrl ?>'>Get Your Divorce Magazine By Mail - Click Here</a>
</h2><a href='<?= $theUrl ?>'>
<img src='..'; width='100%'; height='100%';></a><br><br>
<div id="future" style='width:100%; height:auto;'>
<!-- use CSS stylesheets over inline styles -->
<div id="turnery" class="somethingRelevant">
<div class="featuredpost">
<?= htmlentities($thePostBody) ?>
<?php /* some php stuff way down here */ ?>
Better use a here-doc for such a string:
//add the cover page sales link here
echo <<<saleslink
<h2 class='posttitle'>
<a href='http://divorcemagazinecanada.com/buy-divorce-magazine-online'>Get Your Divorce Magazine By Mail - Click Here</a>
</h2><a href='http://divorcemagazinecanada.com/buy-divorce-magazine-online'>
<img src='http://divorcemagazinecanada.com/wp-content/uploads/2014/07/Divorce.mag_.cover_.jpg'; width='100%'; height='100%';></a><br><br>
<div id="future" style='width:100%; height:auto;'>
<div id="turnery" style="width:280px; height:auto; border:1px solid #fff; float:left; padding:5px; position:relative;">
<div class="featuredpost">
saleslink;
beware that saleslink;needs to be in col 1 of the line!
Take a look at how StackOverflow has formatted your code example. See how the text is red up until the quote before future? That's because it's a string up until that point... where the string is ended because your quote mark is actually ending the quote.
Escape your quotes with \.
<div id=\"future\" ...
You can also just use a single quote out front. This is preferred anyway as it is marginally faster since string interpolation isn't done. If you do this, remember to escape any literal single quotes with \ as well.
<div id="future" style='width:100%; height:auto;'>
<div id="turnery" style="width:280px; height:auto; border:1px solid #fff; float:left; padding:5px; position:relative;">
<div class="featuredpost">
change it to
<div id='future' style='width:100%; height:auto;'>
<div id='turnery' style='width:280px; height:auto; border:1px solid #fff; float:left; padding:5px; position:relative;'>
<div class='featuredpost'>
When you are echoing, using ", you should use ' to internal codes and if it is necessary to use ", use it as \"