I am working on a website in which I want to hide text (Hello World, Point A, and Point B) when serialized string is empty. Below is my code:
<p class="font-weight-bold">Hello World</p>
<p class="font-weight-bold">Point A</p>
<?php
$serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0)
{
$serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($serialized != '')
{
$unserialized = unserialize( $serialized );
foreach($unserialized as $key=>$value)
{
echo $key." ".$value['start']." ".$value['end']."<br/>";
}
}
?>
<p class="mt-2 font-weight-bold text-center">Point B</p>
<?php
$serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0)
{
$serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($serialized != '')
{
$unserialized = unserialize( $serialized );
foreach($unserialized as $key=>$value)
{
echo $key." ".$value['start']." ".$value['end']."<br/>";
}
}
?>
Problem Statement:
Inside Hello World, I have two sections - Point A and Point B.
If both Point A and Point B are empty (meaning serialized string is empty) and there is no data to show then I don't want to show <p> text for Hello World, Point A and Point B at all.
But if either of them is present (meaning serialized string is not empty) then I want to show <p> text for Hello World and <p> text for whatever it is present (it can be Point A or Point B).
Is this possible to do? When the serialize string is empty it is displaying like this:
I don't want those texts to print when the serialized string is empty for Point A or Point B.
You should put the html output in several vars (including the 'Hello World') and do the output at the end when you did all the testing.
<?php
// pre-set/initialize some html templates/strings to be used later
$html = '';
$helloWorld_header = '<p class="font-weight-bold">Hello World</p>';
$pointA_header = '<p class="font-weight-bold">Point A</p>';
$pointA_content = '';
$pointB_header = '<p class="mt-2 font-weight-bold text-center">Point B</p>';
$pointB_content = '';
$pointA_found = $pointB_found = false; // initialize both to false
$pointA_serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "pickup") == 0) {
$pointA_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($pointA_serialized != '') {
$pointA_found = true;
$unserialized = unserialize( $pointA_serialized );
foreach($unserialized as $key=>$value) {
$pointA_content .= $key." ".$value['start']." ".$value['end']."<br/>";
}
// add everything from pointA to $html
$html .= $pointA_header . $pointA_content;
}
$pointB_serialized = '';
for ($i = 0; $i < count($data['item']->process_out); $i++) {
if(strcmp($data['item']->process_out[$i]->processs_type, "location_pickup") == 0) {
$pointB_serialized .= strtolower($data['item']->process_out[$i]->processs_times);
}
}
if($pointB_serialized != '') {
$pointB_found = true;
$unserialized = unserialize( $pointB_serialized );
foreach($unserialized as $key=>$value) {
$pointB_content . $key." ".$value['start']." ".$value['end']."<br/>";
}
// add everything from pointB to $html
$html .= $pointB_header . $pointB_content;
}
// if pointA or pointB is set/found...
if($pointA_found || $pointB_found) {
// ... add the main header before the rest
$html = $helloWorld_header + $html;
}
// do the actual output
echo $html
?>
This is just a quick fix of what you've got. There would be more elegant ways to do that. I'd put pointA and pointB in a shared function, as you mostly do twice the same thing.
Related
I'm trying to strip all the dots and then get the number, and NAME[X] as an output.
My output is:
NAME..................................................................................................3
NAME2...................................................................................................24
NAME3...............................................................................................................................................5
NAME4.......................347
NAME5............................................................................................7
NAME6......................................................................9
I've tried something like this so far:
function introExcerpt($id = null, $introExcerptCut = null)
{
$fileInfo['intro'] = 'my string';
$introExcerpt = trim($fileInfo['intro']);
$lines = preg_split('/\r\n|\r|\n/', $introExcerpt);
$intro = '<div class="toc"><ul class="toc">';
for ($i = 0; $i < count($lines); $i++) {
// if (isset($lines[$i]) && substr(trim($lines[$i]), -1) !== '.') {
$intro.= $lines[$i].'<br />';
//}
}
$intro .= '</div></ul>';
return $intro;
}
Not sure exactly what your output should look like, but you may try just running preg_replace directly on the variable containing all lines:
$lines = preg_replace("/(NAME\d+)\.+(\d+)/", "$1[$2]", $lines);
This would generate the following output based on your sample input:
NAME[3]
NAME2[24]
NAME3[5]
NAME4[347]
NAME5[7]
NAME6[9]
You can use the following function:
function introExcerpt($str, $id = null, $introExcerptCut = null)
{
$fileInfo['intro'] = $str;
$introExcerpt = trim($fileInfo['intro']);
$lines = preg_split('/\r\n|\r|\n/', $introExcerpt);
$intro = '<div class="toc"><ul class="toc">';
for ($i = 0; $i < count($lines); $i++) {
$intro .= '<li>';
$tmpLineArray = explode('.', $lines[$i]);
array_filter($tmpLineArray, function ($value) {
return !is_null($value) && $value != '';
});
foreach ($tmpLineArray as $value) {
$intro .= $value . ' ';
}
$intro .= '</li>';
}
$intro .= '</ul></div>';
return $intro;
}
It is splitting the entire row into array using dot as separator and filters out the empty elements.
Upon further testing the provided solution to my problem which was to word-wrap text inside of the html tags and keep the original enclosing tags such as a <p> or a <b> or even an <i> tag around the words cut at a set position. This is useful for certain application that require a specific formatting to function for the front end. I have modified the code provided to try to achieve this however I have been unsuccessful with the challenging content used in this php example.
I could really use some assistance and I'm sure others could use this information to build dynamic html based books like I am trying to do. it isn't restricted to books there are also possibilities of sliders and other implementations. by not keeping the end tags around each split you are limited to splitting codes that don't break the surrounding code such as <br> tags. I need to split using a </div><div> in that order to close the surrounding tag and reopen it for each page used by another javascript snippet to render each page in a flip book manner.
This is the code I have so far along with sample data:
<?php
function htmlWrapThing($str, $size, $breaking){
$html = false;
$i = 0;
$t = 0;
$tagcount = 0;
$chars = str_split($str);
$return = "";
$break = false;
$tag = "";
$newtag="";
foreach($chars as $char){
if($char=="<"){
$tagcount++;
$html = true;
}
if($char==">") $html = false;
if(($tagcount/2)>1){
$tagcount = 0;
$tag = "";
}
if($html) $t++;
if($html && $char!="/" && $t > 1){
$tag .= $char;
$t =0;
}
if(!$html) $i++;
$return .= $char;
if($i==$size) $break = true;
if($char == " " && !$html && $break){
if(!isset($tag)||$tag==""){
$return .= $breaking;
}else{
$return .= '</'.$tag.'>'.$breaking.'<'.$tag.'>';
}
$i=0;
$break = false;
}
}
return $return;
}
$str = "<h1>hilo everybody how is everyone doing tonight?</h1><p>hello world how is everyone doing today</p><p>hello world how is everyone doing today</p><p>hello world how is everyone doing today</p><br><br />hello everybody how are you doing today?";
echo '<div class="pagecontent">'.htmlWrapThing($str,10, '</div><div class="pagecontent">').'</div>';
This generates output like this:
<div class="pagecontent"><h1>hilo everybody </h></div><div class="pagecontent"><h>how is everyone </h></div><div class="pagecontent"><h>doing tonight?</h1><p>hello </<></div><div class="pagecontent"><<>world how </<></div><div class="pagecontent"><<>is everyone </<></div><div class="pagecontent"><<>doing today</p><p>hello </<<pp></div><div class="pagecontent"><<<pp>world how </<<pp></div><div class="pagecontent"><<<pp>is everyone </<<pp></div><div class="pagecontent"><<<pp>doing today</p><p>hello </pp></div><div class="pagecontent"><pp>world how </pp></div><div class="pagecontent"><pp>is everyone </pp></div><div class="pagecontent"><pp>doing today</p><br><br />hello </b<r></div><div class="pagecontent"><b<r>everybody </b<r></div><div class="pagecontent"><b<r>how are you </b<r></div><div class="pagecontent"><b<r>doing today?</div>
When I need it to be more like this:
<div class="pagecontent"><h1>hilo everybody </h1></div>
<div class="pagecontent"><h1>1how is everyone </h1></div>
<div class="pagecontent"><h1>doing tonight?</h1><p>hello </p></div>
<div class="pagecontent"><p>world how </p></div>
<div class="pagecontent"><p>is everyone </p></div>
<div class="pagecontent"><p>doing today</p><p>hello </p></div>
<div class="pagecontent"><p>world how </p></div>
<div class="pagecontent"><p>is everyone </p></div>
<div class="pagecontent"><p>doing today</p><p>hello </p></div>
<div class="pagecontent"><p>world how </p></div>
<div class="pagecontent"><p>is everyone </p></div>
<div class="pagecontent"><p>doing today</p><br><br />hello</div><div class="pagecontent">everybody </div>
<div class="pagecontent">how are you</div>
<div class="pagecontent">doing today?</div>
As you can see it is getting the reinserted tags all messed up. any ideas how to fix this?
Updated so it doesn't break in the middle of a word.
I just threw this together. Seems to work fine with your test string.
Here's a PHP-fiddle-type-thing
function htmlWrapThing($str, $size){
$html = false;
$i = 0;
$chars = str_split($str);
$return = "";
$break = false;
foreach($chars as $char){
if($char=="<") $html = true;
if($char==">") $html = false;
if(!$html) $i++; $return .= $char;
if($i==$size) $break = true;
if($char == " " && !$html && $break){
$return .= "<br>";
$i=0; $break = false;
}
}
return $return;
}
Just for fun, here's that same function slightly modified for use in JS. And a JSFiddle, too.
function HtmlTrimThing($chars, $size){
$html = false;
$break = false;
$i = 0;
$return = "";
$c = $d = $chars.length;
while($c--){
$char = $chars[$d-$c-1];
if($char=="<") $html = true;
if($char==">") $html = false;
if(!$html) $i++;
$return += $char;
if($i==$size) $break = true;
if($break && $char == " " && !$html){
$return += "<br>";
$i=0;
$break = false;
}
}
return $return;
}
I'm using old fashion way (kickstarter) to build extension in TYPO3. I would like to ad some PHP code after third element of list, but I really don't know how to do this.
My code looks like that:
protected function makeList($res) {
$items = array();
// Make list table rows
while (($this->internal['currentRow'] = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
$items[] = $this->makeListItem();
}
$out = '<div' . $this->pi_classParam('listrow') . '>list items</div>';
return $out;
}
And:
protected function makeListItem() {
$out = 'list item details';
return $out;
}
If I understood correctly, you would need something like this:
protected function makeList($res) {
$items = array();
// Make list table rows
$i = 0;
$out = '<div' . $this->pi_classParam('listrow') . '>';
while (($this->internal['currentRow'] = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
$out .= $this->makeListItem();
$i++;
if ($i == 3) {
$out .= '<img src="whateverjpg">';
$i = 0; // if you want to do it every 3 images
}
}
$out .= '</div>';
return $out;
}
I'm using this script to list a few Twitch.tv streams and their status (offline or online).
If there are no online streams found, I want it to display a text saying that all are offline.
Code that checks if the added streams are online:
//get's member names from stream url's and checks for online members
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
//checks if player streams are online
function onlinecheck($online, $viewers)
{
//If the variable online is not equal to null, there is a good change this person is currently streaming
if ($online != null)
{
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
Full code:
<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php
$members = array("ncl_tv");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
function onlinecheck($online, $viewers) {
if ($online != null) {
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
$alloffline = "All female user streams are currently offline.";
function signin($person){
if($person != null){
return $person;
}
?>
</body>
</html>
............................................................................................................................................................................
Is it because your $userGrab URL contains usernames twice? This is the URL whose contents you're retrieving:
http://api.justin.tv/api/stream/list.json?channel=painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv,painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv
Having looked at the response, it doesn't look like it's causing the problem. The strange URL is a result of you appending to the $userGrab string in the first foreach loop, after you've already added them with the implode() function call before. I think twitch.tv is rightly ignoring duplicate channels.
If all the values in $checkedOnline are null, everyone is offline. Put this at the end of your first code sample:
$personOnline = false;
foreach($checkedOnline as $person) {
if($person !== null) {
$personOnline = true;
break;
}
}
if(!$personOnline) {
echo 'No one is online';
}
else {
//there is at least someone online
}
It seems that I'm stuck with my recursive function.
I have a problem with closing the unnamed list (</ul>) and the list-items (</li>)
The thing what i get is
-aaa
-bbb
-b11
-b22
-b33
-ccc
-c11
-c22
-c33
-ddd
-d11
-d22
-d33
-eee
-fff
And the thing what i want is:
-aaa
-bbb
-b11
-b22
-b2a
-b2c
-b2b
-b33
-ccc
-c11
-c22
-c33
-c2a
-c2c
-c2c1
-c2c2
-c2b
-ddd
-d11
-d22
-d33
-eee
-fff
This is the code that i'm using
$html .= '<ul>';
$i = 0;
foreach ($result as $item)
{
$html .= "<li>$item->id";
$html .= getSubjects($item->id, NULL, "",$i); <--- start
$html .= "</li>";
}
$html .= '</ul>';
And the function
function getSubjects($chapter_id = NULL, $subject_id = NULL, $string = '', $i = 0 ) {
$i++;
// getting the information out of the database
// Depending of his parent was a chapter or a subject
$query = db_select('course_subject', 'su');
//JOIN node with users
$query->join('course_general_info', 'g', 'su.general_info_id = g.id');
// If his parent was a chapter - get all the values where chapter id = ...
if ($chapter_id != NULL) {
$query
->fields('g', array('short_title', 'general_id'))
->fields('su', array('id'))
->condition('su.chapter_id', $chapter_id, '=');
$result = $query->execute();
}
// if the parent is a subject -
// get value all the values where subject id = ...
else {
$query
->fields('g', array('short_title', 'general_id'))
->fields('su', array('id'))
->condition('su.subject_id', $subject_id, '=');
$result = $query->execute();
}
// Because count doesn't work (drupal)
$int = 0;
foreach ($result as $t) {
$int++;
}
// if there no values in result - than return the string
if ($int == 0) {
return $string;
}
else {
// Creating a new <ul>
$string .= "<ul>";
foreach ($result as $item) {
// change the id's
$subject_id = $item->id;
$chapter_id = NULL;
// and set the string --> with the function to his own function
$string .= "<li>$item->short_title - id - $item->id ";
getSubjects(NULL, $subject_id, $string, $i);
$string .="</li>";
}
$string .= "</ul>";
}
// I thougt that this return wasn't necessary
return $string;
}
Does someone have more experience with this kind of things?
All help is welcome.
I am not sure what you are trying to do but here is some code you can test and see if it helps to solve your problem:
This part is just for testing, it makes three dimensional array for testing:
for ($x = 0; $x < 2; $x++) {
$result["c$x"] = "ROOT-{$x}";
for ($y = 0; $y < 3; $y++) {
$result[$x]["c$y"] = "SECOND-{$x}-{$y}";
$rnd_count1 = rand(0,3);
for ($z = 0; $z < $rnd_count1; $z++) {
$result[$x][$y]["c$z"] = "RND-{$x}-{$y}-{$z}";
$rnd_count2 = rand(0,4);
for ($c = 0; $c < $rnd_count2; $c++) {
$result[$x][$y][$z][$c] = "LAST-{$x}-{$y}-{$z}-{$c}";
}
}
}
}
// $result is now four dimensional array with some values
// Last two levels gets random count starting from 0 items.
UPDATE:
Added some randomness and fourth level to test array.
And here is function which sorts array to unordered list:
function recursive(array $array, $list_open = false){
foreach ($array as $item) {
if (is_array($item)) {
$html .= "<ul>\n";
$html .= recursive($item, true);
$html .= "</ul>\n";
$list_open = false;
} else {
if (!$list_open) {
$html .= "<ul>\n";
$list_open = true;
}
$html .= "\t<li>$item</li>\n";
}
}
if ($list_open) $html .= "</ul>\n";
return $html;
}
// Then test run, output results to page:
echo recursive($result);
UPDATE:
Now it should open and close <ul> tags properly.