limit the word count and add a read more link - php

Ok i have this code so far which is in my wordpress template so precisely this a wordpress stuff.
<?php
$post_id = 266;
echo "<div id='widgets-wrapper3'><div id='marginwidgets' style='overflow: auto; max-width: 100%; margin: 0 auto; border: none !important;'>";
$queried_post = get_post($post_id);
echo "<div class='thewidgets'>";
echo $queried_post->post_content;
echo '</div>';
echo "</div></div>";
?>
as you can see into the code, the routine is, to display the post which has an id of 266, now all i want is to limit the word count in the post content of that post, let say I want to limit the word to 300 and then add a read more link. how to make that please?
hope there's someone here who figured out how to make that.
I am open in, Ideas, recommendation and suggestion. Hope someone here could help, thank you.

try this:
http://codex.wordpress.org/Function_Reference/the_excerpt
or use php substr:
echo get_sub($queried_post->post_content, 300);
function get_sub($str, $max=300)
{
$ar = explode($str);
$count = 0;
$new_str = "";
$del = " ";
foreach($ar as $a)
{
if($count == 0)
{
//no space
$del = "";
}
if($count < $max)
{
$new_str .= $del.$a;
}
$count++;
}
return $new_str;
}
if the content contains html elements, its a problem.
hope it helps

Related

Letting CSS interact with HTML through PHP

The following is my PHP code:
$heads = array();
$heads[0] = "<p class='ourmoto'>Your camera solutions...</p>";
$heads[1] = "<a href='index.html'><img id='rentals' src='images/rentalsnew2.jpg' alt='GroupLogo' /></a>";
$heads[2] = "<p class='ourmoto'>...Your camera solutions</p>";
for ($i = 0; $i<3; $i++){
echo"<p>$heads[$i]</p>";
}
CSS:
.ourmoto {
font-style:oblique;
font-size:30px;
display:inline;
margin: 0 auto;
}
From my knowledge, echo should print whatever comes after it in a quote as a string, even if its HTML tags. It manages to print the HTML but CSS won't apply on it. What am I doing wrong?
Thank you!
Iterate through each heads, echo paragraph with class:
foreach ($heads as $head)
echo "<p class='ourmoto'>{$head}</p>";
}

PHP sort not working in Chrome and Safari

I am not sure if it is the sort that isn't working or the way that I am outputting the information. But it would seem that the order that these "li" elements are being made is wrong sometimes.
The images in the folder are named something like
A-Mike-groomsman-topRight-light.jpg
B-James-groomsman-topRight-light.jpg
C-Jared-groomsman-topRight-light.jpg
Code is below. The "li" are in the right order in Firefox, but Chrome and Safari sometimes they put the last one first. Then sometimes they don't. Though I wonder if it could be the bxslider moving things around after the page load? Anyone experience this before?
<?PHP
$titleName = 'who\'s who'; //Wording for title of this section. Change this if you want to change the title text of this section
include 'modules/title.php';
$boydirectory = $_SERVER['DOCUMENT_ROOT']."/resources/images/who/boys";
$girldirectory = $_SERVER['DOCUMENT_ROOT']."/resources/images/who/girls";
$boy_results_array = array();
$girl_results_array = array();
if (is_dir($boydirectory))
{
if ($handle = opendir($boydirectory))
{
foreach(glob($boydirectory.'/*.*') as $file)
{
$boy_results_array[] = basename($file);
}
closedir($handle);
}
}
if (is_dir($girldirectory))
{
if ($handle = opendir($girldirectory))
{
foreach(glob($girldirectory.'/*.*') as $file)
{
$girl_results_array[] = basename($file);
}
closedir($handle);
}
}
sort($boy_results_array);
sort($girl_results_array);
?>
<div class="whoSlider boy">
<h3>Boys</h3>
<ul class="whoBoysbxslider">
<?php
if(count($boy_results_array) > 0){
for ($i = 0; $i < count($boy_results_array); $i++) {
$result = explode('-', $boy_results_array[$i]);
$name = str_replace("_", " ", $result[1]);
$job = str_replace("_", " ", $result[2]);
$alignment = $result[3];
$color = str_replace(".jpg", "", $result[4]);
echo "<li>";
echo "<img src=\"../resources/images/who/boys/$boy_results_array[$i]\" />";
echo "<div class=\"captionContainer $alignment $color\">";
echo "<span>$name</span>";
echo "<span>$job</span>";
echo "</div></li>";
}
}
?>
</ul>
</div>
<div class="whoSlider girl">
<h3>Girls</h3>
<ul class="whoGirlsbxslider">
<?php
if(count($girl_results_array) > 0){
for ($j = 0; $j < count($girl_results_array); $j++) {
$result = explode('-', $girl_results_array[$j]);
$name = str_replace("_", " ", $result[1]);
$job = str_replace("_", " ", $result[2]);
$alignment = $result[3];
$color = str_replace(".jpg", "", $result[4]);
echo "<li>";
echo "<img src=\"../resources/images/who/girls/$girl_results_array[$j]\" />";
echo "<div class=\"captionContainer $alignment $color\">";
echo "<span>$name</span>";
echo "<span>$job</span>";
echo "</div></li>";
}
}
?>
</ul>
</div>
Apparently the issue was not with php but with the BX slider. The issue is it was going to the clone slide instead of the first slide. Only in chrome and Safari. The below link talks about the issue.
https://github.com/stevenwanderski/bxslider-4/issues/154
The solution in there that worked for me was adding this to the jquery.bxslider.css file
.bx-viewport li { min-height: 1px; min-width: 1px; }
try
flush()
after echo() to make sure the content you want to print is sent to the client at that point.
First of all, PHP is a server side language and the behaviour is not effected by the browser.
So I would say that your issue is caused by bxslider which is a jQuery plugin so the behaviour it could be affected by the browser.
To check that you can press ctrl+u in firefox and chrome and see that the html is the same.

Loop through database for a particular record and show results - PHP

i'm looking for a way to loop through the database and shows results in a html page, i'm actually creating a sorta fake email system for a little project.
so i have a table name "SENDS" with 5 columns ID, Mailtext, Touser, Subject, Fromuser.
what i wanna do is when user log in system connects to the database and select everything
from SENDS where touser='$email' -$email is defined when user logs in- and display it in a html div
<div class="oubx">
<div class="inbx">
<span class="from"></span>
<span class="subject"></span>
<span class="mailtext"></span>
</div>
</div>
.oubx {
height:27px; width:800px;
border:1px solid black;
margin-top:;
margin-left:80px;
float:left;
background-color:white;
}
.inbx {
height:22px; width:700px;
border:1px solid black;
margin-top:1px;
margin-left:45px;
background-color:white;
font-family:sans-serif;
font-weight:bold;
font-size:16px;
color:#808080;
float:left;
}
from of course tells whos it from
subject is subject
mailtext is what the mail is (it's hidden of course jQ will take care of it once clicked)
i have used mysqli_num_rows, mysqli_fetch_array, for loops, foreach etc but i just can't seem to get my head around it i'm having a hard time figuring out how to do it setp by steps or what the step is. please help :) thanks.
$connectDB = mysqli_connect("host","root","","db");
$touser = mysqli_query($connectDB, "select * from sends where touser='$email'");
$to_numrows = mysqli_num_rows($touser);
$to_fetch = mysqli_fetch_array($touser);
//printing of html code = how many rows
$ib = '<div class="oubx"><div class="inbx"><span class="from">'.$value.'</span><span class="subject">'.$value.'</span></div></div>';
for ($i = 0; $i < $to_numrows; $i++) {
echo $ib;
}
the rest is work in progress..
Try this,
$connectDB = mysqli_connect("host","root","","db");
$touser = mysqli_query($connectDB, "select * from sends where touser = '$email'");
$all_rows = mysqli_fetch_all($touser, MYSQLI_ASSOC);
$ib = '';
foreach($all_rows as $row) {
$ib .= '<div class="oubx">'
. '<div class="inbx">'
. '<span class="from">' . $row['Fromuser'] . '</span>'
. '<span class="subject">' . $row['Subject'] . '</span>'
. '</div>'
. '</div>';
}
echo $ib;
1- mysqli_fetch_all will fetch all result row into a PHP array
2- We loop through it with foreach (no need to know the rows count)
3- Foreach row, we append the HTML to the $ib intially set to empty string.
Hope it helps.
put the query result in a variable:
while ($row = $touser->fetch_array()) {
$rows[] = $row;
}
and iterate it with a loop:
foreach ($rows as $row) {
/* show appropriate html here */
}

How to go to an end of recordset using php?

Here is the code:
<?php
require'connection.php';
$cons = mysql_query( 'SELECT Subject FROM tblsubject' );
$total_rows = mysql_num_rows($cons);
$counter = 0;
$fields="";
while($row = mysql_fetch_array($cons)) {
if(++$counter == $total_rows) {
$fields .= $row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width: \'20% \' <br/>}<br/>';
}
else {
$fields = $row['Subject'].': { <br/> title: \''.$row["Subject"].'\',<br/> width: \'20% \' <br/>},<br/>';
}
}
echo $fields;
?>
The code stated above should produce an output like this below:
General: {
title:'General',
width: '20%',
},
Math: {
title:'Math',
width: '20%',
}
But if the query reaches its last row, the "," should be omitted after the last "}".
I'm getting it right but when I echo it outside the while loop, only the two last row on the database are echoed.
Can someone help me with this? Please tell me where I am going wrong. Thank you very much :D
I need to echo all the data in the table not just the final two.
Don't even bother with a complicated state checker. Just build an array and then implode it. Automatic "proper" number of commas.
while($row = mysql_fetch_array()) {
$array[]= $row['data_you_want'];
}
echo implode(',', $array);
You could try something like this:
$print = '';
while($row = mysql_fetch_array($cons)) {
$row_subject_array[] = $row['Subject'];
}
for($i = 0; $i < count($row_subject_array); $i++) {
$print .= $row_subject_array[$i]. ': { <br/> title: \''.$row_subject_array[$i].'\',<br/> width: \'20% \' <br/>},';
}
$print = rtrim($print, ',');
echo "<br />";
Also a little suggestion: You should stop of use mysql_x() functions. These functions are deprecated, instead of it you can use mysqli_x() function or PDO.
Why shouldn't I use mysql_* functions in PHP?
Hello and Welcome to Stack Overflow!
First of all you should avoid using the mysql class and use mysqli as mysql has been deprecated.
Now for your question, If your MySQL table has an ID column with Auto Increment enabled then you can do this:
SELECT id FROM table ORDER BY id DESC LIMIT 1
However if you want to achieve the same result then I would format my code in the following way:
$cons = mysql_query( 'SELECT Subject FROM tblsubject' );
$fieldsArray = array();
while($row = mysql_fetch_array($cons)) {
array_push($fields_array,$row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width: \'20% \' <br/>},<br/>');
}
array_pop($fieldsArray);
array_push($fieldsArray,$row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width: \'20% \' <br/>}<br/>');
foreach($fieldsArray as $value) {
echo $value;
}
tada!
i always do:
$var = substr($var,0,-1);
echo $var; //or whatever

Show div depending on user logged in

$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
$dellink="Delete";
$editlink="Edit";
$admin = "MJH";
if(($instby == $username)||($instby == $admin))
{
echo "<div id=\"editcont\">$editlink $dellink</div>";
}
else if($inst == $username)
{
echo "<div id=\"editcont\">$editlink <font face=\"Trebuchet MS, Arial, Helvetica, sans-serif\" size=\"2\">Delete</font></div>"
}
else
{
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
.in my code above what i want to do is to show a containing links depending on who is logged in.. however, when the admin logs in he will be able to see the containing $editlink and $dellink.
.I can't seem to find where exactly have i gone wrong.. please help me with this guys! TIA! More Power!
.this is what my code looks like.
if(($instby == $username)||($username == $admin))
$admin = "MJH";
if(($instby == $username)||($instby == $admin)) {
if($inst == $username) {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcont\">$editlink <font>Delete</font></div>";
}
else {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
}
Thats how i percieved you wanted it, but really, it is very hard to understand whats going on here. Are $inst and $instby meant to be different variables? Also, out of curiousity, why have you double bracketed your if(); statements?
Hope I deciphered it correctly.
PS. I don't think echo();ing html is good practice.

Categories