Help adding unique text to each page displayed


#1

I am using a php web video script which displays video pages titled: ‘trending’, ‘latest’ and ‘top’.
The php file code below generates what the html code will display, I believe. The outcome is if ‘trending’ is chosen, display that list of videos, if ‘latest’ is chosen display that list of videos, etc…
I would like to add some unique text to each page, but what is displayed in each choice is simply a new ‘title’ and a new list of videos. How could I either separate each page to add unique text to each page, or how would I add code to show unique text to appear when the unique page is displayed, using the existing code. For example, more ‘trending’ text when the ‘trending’ page is displayed, and more ‘latest’ text when the ‘latest’ page is displayed. I hope that make sense.

Here is the php code:

<?php

if (empty($_GET['page'])) {
    header("Location: " . PT_Link('404'));
    exit();
}
$page         = PT_Secure($_GET['page']);
$limit        = 20;
$pt->rss_feed = false;
$pt->exp_feed = true;
$pages        = array(
    'trending',
    'category',
    'latest',
    'top'
);

if (!in_array($page, $pages)) {
    header("Location: " . PT_Link('404'));
    exit();
}

if (!empty($_GET['feed']) && $_GET['feed'] == 'rss') {
    $limit        = 50;
    $pt->rss_feed = true;

}

$cateogry_id = '';
$videos = array();
if ($page == 'trending') {
    $title  = $lang->trending;
    $db->where('privacy', 0);
    $videos = $db->where('time', time() - 172800, '>')->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
}

else if ($page == 'latest') {
    $title  = $lang->latest_videos;
    $db->where('privacy', 0);
    $videos = $db->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
}

else if ($page == 'top') {
    $title  = $lang->top_videos;
    $db->where('privacy', 0);
    $videos = $db->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
}

else if ($page == 'category') {
    if (!empty($_GET['id'])) {
        if (in_array($_GET['id'], array_keys($categories))) {
            $cateogry = PT_Secure($_GET['id']);
            $title    = $categories[$cateogry];
            $cateogry_id = "data-category='$cateogry'";
            $db->where('privacy', 0);
            $videos   = $db->where('category_id', $cateogry)->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
        } else {
            header("Location: " . PT_Link('404'));
            exit();
        }
    }
}

use Bhaktaraz\RSSGenerator\Item;
use Bhaktaraz\RSSGenerator\Feed;
use Bhaktaraz\RSSGenerator\Channel;


//Export rss feed
if ($pt->rss_feed) {
    $rss_feed_xml   = "";
    $fl_rss_feed    = new Feed();
    $fl_rss_channel = new Channel();


    $fl_rss_channel
        ->title($pt->config->title)
        ->description($pt->config->description)
        ->url($pt->config->site_url)
        ->appendTo($fl_rss_feed);

    if (is_array($videos)) {
        foreach ($videos as $feed_item_data) {
            $feed_item_data = PT_GetVideoByID($feed_item_data, 0, 0, 0);
            $fl_rss_item    = new Item();
            $fl_rss_item
             ->title($feed_item_data->title)
             ->description($feed_item_data->markup_description)
             ->url($feed_item_data->url)
             ->pubDate($feed_item_data->time)
             ->guid($feed_item_data->url,true)
             ->media(array(
                'attr'  => 'url',
                'ns'    => 'thumbnail',
                'link'  => PT_GetMedia($feed_item_data->org_thumbnail)))
             ->appendTo($fl_rss_channel);
        }
    }

    header('Content-type: text/rss+xml');
    echo($fl_rss_feed);
    exit();
}


$html_videos = '';
if (!empty($videos)) {
    foreach ($videos as $key => $video) {
    	$video = PT_GetVideoByID($video, 0, 0, 0);
        $html_videos .= PT_LoadPage('videos/list', array(
            'ID' => $video->id,
            'VID_ID' => $video->id,
	        'TITLE' => $video->title,
	        'VIEWS' => $video->views,
            'VIEWS_NUM' => number_format($video->views),
	        'USER_DATA' => $video->owner,
	        'THUMBNAIL' => $video->thumbnail,
	        'URL' => $video->url,
	        'TIME' => $video->time_ago,
            'DURATION' => $video->duration
        ));
    }
}

if (empty($videos)) {
	$html_videos = '<div class="text-center no-content-found empty_state"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-video-off"><path d="M16 16v1a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2m5.66 0H14a2 2 0 0 1 2 2v3.34l1 1L23 7v10"></path><line x1="1" y1="1" x2="23" y2="23"></line></svg>' . $lang->no_videos_found_for_now . '</div>';
}
$pt->videos_count= count($videos);
$pt->page        = $page;
$pt->title       = $title . ' | ' . $pt->config->title;
$pt->description = $pt->config->description;
$pt->keyword     = @$pt->config->keyword;
$pt->content     = PT_LoadPage('videos/content', array(
    'TITLE' => $title,
    'VIDEOS' => $html_videos,
    'TYPE' => $page,
    'CATEGORY_ID' => $category_id
));

and the accompanying html code:

<div class="wo_about_wrapper_parent">
	<div class="wo_about_wrapper">
		<div class="hero hero-overlay" style="background-color: #033d5d;">
			<div class="container">
				<h1 class="text-center"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-video"><polygon points="23 7 16 12 23 17 23 7"></polygon><rect x="1" y="5" width="15" height="14" rx="2" ry="2"></rect></svg> {{TITLE}}</h1>
			</div>
		</div>
		<svg id="wave" viewBox="0 0 1440 48" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g transform="translate(0.000000, -564.000000)" fill="#033d5d"><g id="Head-background" transform="translate(-168.000000, -346.000000)"><g id="waves-background" transform="translate(888.000000, 934.000000) scale(-1, 1) translate(-888.000000, -934.000000) translate(168.000000, 910.000000)"><path d="M0,14.60779 C101.722445,4.7613277 195.244576,-4.14113188e-14 429,-2.84217094e-14 C732,-1.97619698e-14 798,45.6756592 1089,45.6756592 C1245.83848,45.6756592 1364.64602,29.5028807 1440,14.5904096 C1440,14.60779 1440,48 1440,48 L0,48 C0,48 0.0410082206,34 0,14.60779 Z" id="Path-3-Copy"></path></g></g></g></g></svg>
	</div>
</div>
<div class="content pt_shadow pt_page_margin" style="padding-top: 25px;">
	<div class="col-md-12">

			<div class="upload-head">
				<div style="float: right;">
					<button class="btn btn-main" data-action="multuple-buy-video" onclick="PT_MultipleBuyVideo();">View all selected</button>
				</div>
				<h4><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg></h4>
				<hr>
		</div>

		<div class="videos-latest-list row">
      	{{VIDEOS}}
		</div>
		<?php if ($pt->videos_count > 1) { ?>
			<div class="watch-video-show-more desc load-more" data-type="{{TYPE}}" {{CATEGORY_ID}}>
                {{LANG show_more}}
            </div>
		<?php } ?>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
</div>

Any help will be appreciated


#2

Where will the additional text come from?


#3

Thanks for your reply. Sorry for not being clear enough.
I’d like to add the text to display.

For example, (code below) I added “HELLO TEST” into the current html, but it shows that same text (HELLO TEST) on all displayed pages ( ‘trending’, ‘latest’ and ‘top’.). I’m trying to modify to show unique text for each displayed page.


			<div class="upload-head">
				<div style="float: right;">
					<button class="btn btn-main" data-action="multuple-buy-video" onclick="PT_MultipleBuyVideo();">View all selected</button>
				</div>
				<h4><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg></h4>
				<hr>
		</div>
 <div class="test"><font color="#696969" font size="3" face="Arial">
HELLO TEST</font>
</div>
		<div class="videos-latest-list row">
      	{{VIDEOS}}
		</div>
		<?php if ($pt->videos_count > 1) { ?>
			<div class="watch-video-show-more desc load-more" data-type="{{TYPE}}" {{CATEGORY_ID}}>
                {{LANG show_more}}
            </div>
		<?php } ?>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
</div>

#4

Your top piece of code has a series of if() clauses that decide how to display the videos depending on the category chosen, so you could create a new variable in each of those, assign to it the appropriate text that you want to display, and then display that variable in the correct place. Or, in that correct place, you could have another if() clause (or probably a switch()) to decide what text to display based on the value of $page. Personally I’d prefer the former.

But what I really meant was, will the text come from a database, or will it be hard-coded for each category?


#5

Thanks for your reply.
Yes, hard-coded, not from the db. Thanks for that question.
Can you please share an example of what you mean by “new variable in each of those”.
much thank again


#6

Sorry, I don’t want to do this for you. I know it’s not code you’ve written, but you must by now have picked up enough PHP to know how to create a new variable, assign it a value, and display it on the page. The code you posted shows several examples of how to create a variable and assign it a value.

Give it a go, post what you try and we’ll help to steer you in the right direction.


#7

Thanks again for your reply. I’m not sure what needs correcting here:

$cateogry_id = '';
$videos = array();
if ($page == 'trending') {
    $title  = $lang->trending;
    $db->where('privacy', 0);
    $videos = $db->where('time', time() - 172800, '>')->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
}

else if ($page == 'latest') {
    $title  = $lang->latest_videos;
    **$text1 = "THIS IS A TEST";**
**    echo $text1;**
    $db->where('privacy', 0);
    $videos = $db->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
}

and

			<div class="upload-head">
				<div style="float: right;">
					<button class="btn btn-main" data-action="multuple-buy-video" onclick="PT_MultipleBuyVideo();">View all selected</button>
				</div>
				<h4><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg></h4>
				<hr>
		</div>
 <div class="test"><font color="#696969" font size="3" face="Arial">
HELLO**<?php echo $text1;?>**</font>
</div>
		<div class="videos-latest-list row">
      	{{VIDEOS}}
		</div>
		<?php if ($pt->videos_count > 1) { ?>
			<div class="watch-video-show-more desc load-more" data-type="{{TYPE}}" {{CATEGORY_ID}}>
                {{LANG show_more}}
            </div>
		<?php } ?>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
</div>

Any additional guidance is appreciated


#8

Did you test it and did it render the page ok?

I prefer string a default which is later overwritten.

<?PHP

//
$test = '$test ==>  Big problem'; // notice single quotes

if ($page==='home'):
  $test = 'home';
else if( $page==='contact'):
  $test = 'contact';
else if ( $page==='about'):
  $test='about';
else:
  $test='should never, ever get here'; 
endif;

echo $test;
die; // halts browser execution 

#9

Thanks for your reply.

Regarding “Did you test it and did it render the page ok?”, the page appear without the "“THIS IS A TEST”.

I’m not sure what you’re suggesting. If it is to put single quotes like this:

else if ($page == 'latest') {
    $title  = $lang->latest_videos;
    **$text1 = 'THIS IS A TEST';**
    echo $text1;
    $db->where('privacy', 0);
    $videos = $db->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
}

that still didn’t display THIS IS A TEST on the html page

Any other guidance will be appreciated


#10

Without adding the following lines to the beginning of the script errors and warnings are not shown which is like programming with blinkers!

<?php
declare( strict_types=1);
// these should be set in PHP.ini
error_reporting(-1); // set maximum errors
ini_set('display_errors' , 'true');

// your script goes below

If THIS IS A TEST did not display then I think the above additions will display something like:

undefined variable: $test


#11

Thanks for your reply.

Yes, after adding your suggested code, I see this:

Notice: Undefined variable: text1 in /home/public_html/themes/videos/content.html on line 23

and line 23 is:

<div class="test"><font color="#696969" font size="3" face="Arial">HELLO<?php echo $text1;?></font>

any additional guidance with defining the variable and displaying the text will be welcomed


#12

Try to understand the script given in post #8, don’t forget the error checking script and try numerous tests by changing the $page value.


#13

Thanks again I’ve changed the page value and now see this on the html page:

HELLO$text1

is this correct code?:

 <div class="test"><font color="#696969" font size="3" face="Arial">HELLO<?php echo '$text1';?></font>
</div>

#14

OP, variables will not interpolate within single quotes. In this case no quotes are needed. And stop using deprecated HTML. font and face went out in the 90’s. We use CSS now.


#15

I would also suggest you take some time to get familiar with SOLID principles and start designing code in multiple parts with well defined responsibilities, instead of this transaction script which you’ll find doesn’t scale all that well.


#16

Thanks again I’ve changed the page values and now see this on the html page:

Notice: Undefined variable: text1 in /home/public_html/themes/videos/content.html on line 23

line 23 is this:
<div class="test"HELLO<?php echo $text1;?></div>

the (partial) php is this:

$text1 = 'THIS IS A TEST';
$cateogry_id = '';
$videos = array();
if ($page == 'trending') {
$text1 = 'trending';
    $title  = $lang->trending;
    $db->where('privacy', 0);
    $videos = $db->where('time', time() - 172800, '>')->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
}

any additional help will be appreciated


#17

It seems to me that the reason the variable is showing as undefined is because of the way that the page that defines it passes values to the code that renders the page. I believe it is this bit of code that decides what is passed through:

$pt->content     = PT_LoadPage('videos/content', array(
    'TITLE' => $title,
    'VIDEOS' => $html_videos,
    'TYPE' => $page,
    'CATEGORY_ID' => $category_id
));

and as you can see, it is not passing $text1 through.

So, you either need to modify it to do that, then modify the page to use the contents of the variable, or consider my other suggestion in #4, which was to consider modifying the content page to look at the “TYPE” and assign the text from there.

You can see clearly in the code I posted above which parameters are being sent, what they are called, and how those parameters are referenced in the second bit of code where it renders the page. It should be relatively simple to add another parameter with a new name, give it the value you put in $text1, and then add it to the display. But, it’s clearly using some kind of framework, and I don’t know if it’s as easy as it looks on the face of it.


#19

Thanks so much for your reply. I really appreciate it.
Based on your suggestion, I tried this in the php:

$text1 = 'THIS IS A TEST';
$category_id = '';
$videos = array();

if ($page == 'trending') {
    $title  = $lang->trending;
    $db->where('privacy', 0);
    $videos = $db->where('time', time() - 172800, '>')->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
    $text1 = 'TEXT';
}

along with this:

$pt->content     = PT_LoadPage('videos/content', array(
    'TITLE' => $title,
    'VIDEOS' => $html_videos,
    'TYPE' => $page,
    'CATEGORY_ID' => $category_id,
    'TEXT' => $text1

and have this in the html:

<div class="test">HELLO{{TEXT}}</div>

and that successfully displays:

“HELLO THIS IS A TEST”

Thanks for the help.

As you know my initial intention was to display unique text, when each of three pages appears. When if ($page == ‘trending’)
“unique text 1” displays. if ($page == ‘latest’) than “unique text 2” displays, and if ($page == ‘top’) than “unique text 3” displays. But, then I got that undefined variable error. Now, I’m trying to have the unique text display again.

I just tried this:

$text1 = 'THIS IS A TEST';
$text2 = 'THIS IS TEST 2';
$category_id = '';
$videos = array();

if ($page == 'trending') {
    $title  = $lang->trending;
    $db->where('privacy', 0);
    $videos = $db->where('time', time() - 172800, '>')->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
    $text1 = 'TEXT';
}

else if ($page == 'latest') {
    $title  = $lang->latest_videos;
    $db->where('privacy', 0);
    $videos = $db->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
    $text2 = 'TEXT2';
}


along with this:

$pt->content     = PT_LoadPage('videos/content', array(
    'TITLE' => $title,
    'VIDEOS' => $html_videos,
    'TYPE' => $page,
    'CATEGORY_ID' => $category_id,
    'TEXT' => $text1,
    'TEXT2' => $text2


and this in the html code:

<div class="test">HELLO{{TEXT}}{{TEXT2}}</div>

but, it shows me this on all three displayed pages:

HELLOTHIS IS A TESTTHIS IS TEST 2

Any guidance as to how to display unique text, when each unique page is loaded, is appreciated.


#20

I find only slight differences in the $testvariables and would add the following to make the script easier to understand:

<?php 
$text1 = '  TEST 1: ' .__LINE__;
$text2 = '  TEST 2: ' .__LINE__;
$category_id = '';
$videos = array();

if ($page == 'trending') {
    $title  = $lang->trending;
    $db->where('privacy', 0);
    $videos = $db->where('time', time() - 172800, '>')->orderBy('views', 'DESC')->get(T_VIDEOS, $limit);
    $text1 = ' TEXT trending : ' .__LINE__;
}

else if ($page == 'latest') {
    $title  = $lang->latest_videos;
    $db->where('privacy', 0);
    $videos = $db->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
    $text2 = ' TEXT2 latest : ' .__LINE__;

}else{
    $title  = $lang->latest_videos;
    $db->where('privacy', 0);
    $videos = $db->orderBy('id', 'DESC')->get(T_VIDEOS, $limit);
    $text2 = ' TEXT3 : SHOULD NEVER GET HERE  : ' .__LINE__;
}


// along with this:

$pt->content = PT_LoadPage('videos/content', 
array(
    'TITLE' 	=> $title,
    'VIDEOS' 	=> $html_videos,
    'TYPE' 		=> $page,
    'CATEGORY_ID' => $category_id,
    'TEXT' 		=> $text1 .'  ($test1) ==> ' .__LINE__,
    'TEXT2' 	=> $text2 .'  ($test2) ==> ' .__LINE__
 )
);    

Edit:
Added closing array bracket.


#21

Thank you for your reply, but that created code a blank page.
I’d like to stay with the php I have and look for additional help with displaying unique text, when each unique page is loaded.

Any additional help is welcomed