How to pass dynamic variable through class function - php

I'm trying to pass every value through the function (it's a part of cms) so later on user can view content and then when he confirms it is saved to database and a separate file is made that looks exactly as preview but I don't know and have no idea how to pass a dynamic variable like $_POST['champno'.$i] right below
public function display($patch, $champ_number){
echo '<h1 style="float: left;">PATCH ';
echo $patch.'</h1>
<nav id="header-menu">
<ul>
<li>PATCHES</li>
<li>CHAMPIONS</li>
<li>ITEMS</li>
<li>CHANGES</li>
</ul>
</nav>
<div id="title">
<h2>CHAMPION BALANCE CHANGES</h2>
</div>';
for($i=1;$i<=$champ_number; $i++){
?>
<style type="text/css" scoped>
#media(min-width: 640px){
.<?php echo $_POST['champno'.$i]; ?>{
background-image: url('../../assets/champions/<?php echo $_POST['champno'.$i]; ?>_pc.jpg');

Extract the information from $_POST['champno'.$i] into a dedicated array and call the function with that:
$champions = array();
for ($i = 1; $i <= $champ_number; $i++)
$champions[] = $_POST['champno' . $i];
$obj->display($patch, $champions);
In the function:
function display($patch, $champions) {
...
foreach ($champions as $champion) {
$champion = htmlspecialchars($champion);
?>
<style type="text/css" scoped>
#media(min-width: 640px) {
.<?php echo $champion; ?> {
background-image: url('../../assets/champions/<?php echo $champion; ?>_pc.jpg');
...
<?php
}
}
Then you can use the function with the database and all other input sources.

Related

MPDF Wordpress pull in variables

I am using MPDF with Wordpress and ACF to create vouchers. I want to click a button on my custom post type to generate the pdf (this works). I then want to pull variables into the PDF to fill it with dynamic content from my custom post type and advanced custom fields, it doesn't show errors the values are just blank. This is my code so far:
Button on post:
<a href="<?php get_bloginfo('url'); ?>?offer=<?php echo $post->ID ?>" download>Download The Voucher</a>
Functions.php code to generate the pdf:
add_action('init', 'congres_redirect');
function congres_redirect() {
if(isset($_GET['offer'])) {
$restName = get_field('restaurant_name', $post->ID);
$image = get_field('restaurant_image', $post->ID);
view_conferinta();
}
}
function view_conferinta() {
$output = '<html>
<head><title>Voucher Download</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" />
<style>
.voucher-content {
padding: 20px;
}
.inner-voucher {
padding: 20px;
border: solid 1px #000;
}
</style>
<body>';
$output .= '
<div class="voucher-content" style="font-family:chelvetica">
<div class="inner-voucher">
<img src=".$image." alt="Logo" style=" margin: 0;" />
<h1>Voucher Title</h1>
<p>Voucher Description</p>
<p>Voucher Code: EL-200DEG07022020-123AB</p>
<p>Restaurant Name:'.$restName.'</p>
<p>This is my static voucher template</p>
<p>POST ID:'.$post->ID.'</p>
</div>
</div>
';
$output .= '
</body>
</html>';
require_once __DIR__ . '/mpdf/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf(['debug' => true]);
$mpdf->WriteHTML($output);
$mpdf->Output('my-voucher.pdf','I');
//$mpdf->Output($pdfheader.'.pdf', 'D');
exit;
}
This generates a pdf but all the dynamic content is blank. What am I doing wrong?
You're not passing the dynamic variables into the function creating the output. You also need to declare the $post global
Change
function view_conferinta() {
$output = '<html>
to
function view_conferinta(restName, $image) {
global $post;
$output = '<html>
Then add global post to your init function:
function congres_redirect() {
if(isset($_GET['offer'])) {
global $post; //ADD THIS
$restName = get_field('restaurant_name', $post->ID);
$image = get_field('restaurant_image', $post->ID);
view_conferinta();
}
}

Generate Web Page Using PHP Class

I am a university student experimenting with OOP and PHP. I was wanting to build up a page using a BuildPage class.
<?php
Class BuildPage {
private $title;
private $style;
private $head;
private $header;
private $page;
private $footer;
private $finalPage;
public function __construct($title, $style)
{
$this->title = $title;
$this->style = $style;
}
public function addHead()
{
$this->head = "
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>$this->title</title>
<link href='$this->style' rel='stylesheet' type='text/css'>
<link rel='stylesheet' href='style.css'>
</head>";
}
public function addToPage($partOfPage)
{
if(empty($this->page) || !isset($this->page))
{
$this->page = $partOfPage;
}
else {
$this->page .= "<br> " . $partOfPage;
}
}
public function addHeader($header)
{
$this->header = "<body><div class='container'><header> " . $header . "
</header>";
}
public function addFooter($footer)
{
$this->footer .= "<BR><footer> " . $footer . " </footer></div></body>
</html>";
}
public function getPage()
{
$this->finalPage = $this->head . $this->header . $this->page .
$this->footer;
return $this->finalPage;
}
}
?>
However, when I try to build the page using the functions, I cannot understand how to use PHP within the argument as below:
$buildPage->addToPage("
<!-- Here is our page's main content -->
<!-- Use function to display songs -->
<?php $music->displaySongs('xmlfiles/songs.xml'); ?>
");
If I escape the $ like \$ to try and make it a string, for some reason the becomes a comment in HTML. Is it possible for this approach to work at all?
Many Thanks
EDIT:
This is the index page below, i call the class at the bottom with an echo.
<?php
require_once('phpclasses/Connect.php');
require_once('phpclasses/BuildPage.php');
require_once('phpclasses/MusicIE.php');
$dbconnect = new Connect();
$music = new MusicIE();
$buildPage = new BuildPage("Music Website", "style.css");
$buildPage->addHead();
$buildPage->addHeader("
<!-- Here is the main header that is used accross all the pages of my
website
-->
<div class='PageHeading'>Available Music Listed Below:</div>
<nav>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Register</a></li>
<li><a href='#'>Login</a></li>
</ul>
</nav>
<form>
<input type='search' name='q' placeholder='Search query'>
<input type='submit' value='Go!'>
</form>
");
$buildPage->addToPage("
<!-- Here is our page's main content -->
<!-- Use function to display songs -->
" . $music->displaySongs('xmlfiles/songs.xml'));
$buildPage->addFooter("
<!-- And here is my footer that is used across all the pages of our website
-->
<p>©Copyright 2017 by Kris Wilson. All rights reversed.</p>
");
echo $buildPage->getPage();
?>
Since you are passing a string to the function BuildPage::addToPage($partOfPage) you could try to pass the result of $music->displaySongs('xmlfiles/songs.xml') instead of the literal function call as a string. Like so:
$buildPage->addToPage("
<!-- Here is our page's main content -->
<!-- Use function to display songs -->
". $music->displaySongs('xmlfiles/songs.xml'));
Seems that your code is wrong on the addToPage function call. you should call it like this:
$buildPage->addToPage("
<!-- Here is our page's main content -->
<!-- Use function to display songs -->
".$music->displaySongs('xmlfiles/songs.xml'));
You should add the $music->displaySongs('xmlfiles/songs.xml') output to the end of the string.

How to make visible all my videos with pagination in my Ziggeo application?

So I need some help from a Ziggeo user. I have registered 8 videos in my ziggeo server and now I want to display them in pages divided by 2 videos per page.
Here is what I wrote, but unfortunately it doesn't show me any video, but the compiler doesn't say any error.
<?php include('./ziggeo/pagination.class.php');?>
<?php $myvideos = $ziggeo->videos();
$myarray = array($myvideos);?>
<div class="gallery">
<?php if(count($myarray)){
$pagination = new pagination($myarray, (isset($_GET['page'])?$_GET['page']:1), 3);
$videos = $pagination->getResults();
if(count($videos)!=0) {
echo $pageNumbers = '<h2>'.$pagination->getLinks().'</h2>';
foreach ($videos as $video) {?>
<div class="wall-of-videos-container">
<ziggeo ziggeo-video="<?= $video->token ?>"
ziggeo-width=320 ziggeo-height=240 ziggeo-popup> </ziggeo>
<?= date("Y-m-d h:i a", $video->created) ?>
·<?= $video->duration ?> seconds</div>
<? } echo $pageNumbers; } } ?>
</div><!-- End Gallery -->
I included all the files needed for the Ziggeo configuration.
Who can help me? Thank a lot!
Without seeing 'pagination.class.php' file contents and the output that you are creating it is difficult to know what went wrong, however to create pagination in PHP using Ziggeo PHP SDK, you would do something like this:
<?php
require_once('Ziggeo.php');
$ziggeo = new Ziggeo('YOUR TOKEN', 'YOUR PRIVATE KEY', 'YOUR ENCRYPTION KEY');
?>
You can get the token and keys from your dashboard on ziggeo.com
Ziggeo.php can be retrieved from Ziggeo PHP SDK here: github.com/Ziggeo/ZiggeoPhpSdk
Now looking at your code it seems that this is the call that you are not making correctly. To get the videos you should make the following call:
<?php $myvideos = $ziggeo->videos()->index(); ?>
It is good to remember that by default you will only get up to 50 videos, so if you are expecting to have more than that, you should set the limit parameter.
You can set limit, skip, reverse, states and tags
In case you want to get up to 100 videos (which is maximum per call) you would do something like this:
<?php
$myArguments = array('limit' => 100);
$myvideos = $ziggeo->videos()->index($myArguments);
?>
Now to list them, you would do something like this:
<?php
foreach ($myvideos as $video) {
?>
<ziggeo ziggeo-video="<?php echo $video->token; ?>" ziggeo-width=320 ziggeo-height=240 ziggeo-popup></ziggeo>
<?php
}
?>
You could add a check with count($myvideos) before foreach, however it should not be needed.
In general, to create a page with 2 videos per page you could use something like this:
<?php
$i = 0; //to have two videos per page
$j = 0; //to see how many we have
foreach ($myvideos as $video) {
$j++;
if($i === 0) { ?>
<div class="gallery_page">
<?php } ?>
<ziggeo ziggeo-video="<?php echo $video->token; ?>" ziggeo-width=320 ziggeo-height=240 ziggeo-popup></ziggeo>
<?php
$i++;
if($i === 2) { ?>
</div>
<div class="page_number"><?php echo $j/2; ?> </div>
<?php
$i = 0;
}
}
if($i !== 0) {
?>
<div class="page_number"><?php echo (($j-1)/2)+1; ?> </div>
<?php
}
?>
It is good to point out that the above code is not a complete paging system - it is just a simple sample that shows you how you could do it, however it would need to be customized and worked upon further to match gallery style, and similar.
Looking at your code, I think that it should work using something like this:
<?php
include('./ziggeo/pagination.class.php');
$myvideos = $ziggeo->videos()->index();
?>
<div class="gallery">
<?php
if(count($myvideos)) {
$pagination = new pagination($myarray, (isset($_GET['page']) ? $_GET['page']:1), 3);
$videos = $pagination->getResults();
if(count($videos)!=0) {
echo $pageNumbers = '<h2>'.$pagination->getLinks().'</h2>';
foreach ($videos as $video) { ?>
<div class="wall-of-videos-container">
<ziggeo ziggeo-video="<?php echo $video->token; ?>" ziggeo-width=320 ziggeo-height=240 ziggeo-popup></ziggeo>
<?php echo date("Y-m-d h:i a", $video->created); ?>
·<?php echo $video->duration; ?> seconds</div>
<?php } echo $pageNumbers;
}
} ?>
</div><!-- End Gallery -->
I did presume however that you have the headers set in the HTML HEAD of the page where the gallery will be shown:
<link rel="stylesheet" href="//assets-cdn.ziggeo.com/v1-latest/ziggeo.css" />
<script src="//assets-cdn.ziggeo.com/v1-latest/ziggeo.js"></script>
<script type="text/javascript">ZiggeoApi.token="YOUR TOKEN"</script>
If not present, the HTML code will be created from the above PHP code, however your videos would not be shown due to Ziggeo framework not being loaded on client side.
UPDATE (2016/05/31)
As the above is just general way to do this, it is not including CSS nor JavaScript.
As such I am adding the full code that can be used and as it shows another way to collect the page numbers and leaving the above so that someone can see both.
<script type="text/javascript">
//Basic code needed to switch pages
var currentPage = 1;
function showPage(number) {
//If we are on the same page as the selected one, we just break away from the function, so that we do not hide the same.
if(currentPage === number) { return false; }
var toShow = document.getElementById('page_' + number);
var toHide = document.getElementById('page_' + currentPage);
toShow.style.display = 'block';
toHide.style.display = 'none';
currentPage = number;
}
</script>
<style type="text/css">
/* Code to hide the pages (all) and show first one only, as well as a bit of styling so that it has some basic frame */
.gallery_page > ziggeo {
float: left;
}
.gallery_page {
background-image: linear-gradient(-45deg, lightGray, white);
border-radius: 10px;
box-shadow: 0 0 2px gray;
box-sizing: border-box;
display: none;
min-height: 400px;
margin: 20px 0;
padding: 40px;
width: 720px;
}
.gallery_page:first-child {
display: block;
}
.page_number {
box-shadow: 0 0 3px gray;
float: left;
margin: 0 4px;
text-align: center;
width: 2em;
}
</style>
<div class="gallery">
<?php
//How many videos per page do we want to have?
$numberOfVideos = 2;
//How many videos was there in total?
$totalNumberOfVideos = 0; //only if we need it for something later on
//How many videos are approved / are shown
$totalNumberOfApprovedVideos = 0; //only if we need it for something later on
//which page are we working on?
$currentPage = 1;
//Will serve as buffer for page number elements
$pageNumbers = '';
//temporary videos counter
$i = 0;
foreach ($myvideos as $video) {
//to only show approved videos
if($video->approved === true) {
if($i === 0) { ?>
<div class="gallery_page" id="page_<?php echo $currentPage; ?>">
<?php } ?>
<ziggeo ziggeo-video="<?php echo $video->token; ?>" ziggeo-width=320 ziggeo-height=240 ziggeo-popup></ziggeo>
<?php
$i++;
if($i === $numberOfVideos) { ?>
<br style="float:none; clear:left;">
</div>
<?php
$pageNumbers .= '<div onclick="showPage(' . $currentPage . ');" class="page_number">' . $currentPage . '</div>';
$currentPage++;
$i = 0;
}
$totalNumberOfApprovedVideos++;
$totalNumberOfVideos++;
}
else {
//$video->moderation_reason
//If you want to check if there was a reason why the video was not approved, you can check the above, or alternatively, you could do something else at this point.
$totalNumberOfVideos++;
}
}
if($i !== 0) {
$pageNumbers .= '<div onclick="showPage(' . $currentPage . ')" class="page_number">' . $currentPage . '</div>';
}
?>
</div><!-- End Gallery -->
<?php echo $pageNumbers; ?>
<?php
//This is not needed for pagination to work, however you might want to show it, etc
echo '<br><br>';
echo 'Approved videos: ' . $totalNumberOfApprovedVideos . '<br>';
echo 'Total videos: ' . $totalNumberOfVideos . '<br>';
echo 'Total number of pages: ' . $currentPage . '<br>';
echo $numberOfVideos . ' videos per page<br>';
?>
It is good to point out that this is just a framework - so the code mentioned before will work just as the followup code does, however both require additional styling and code to make it look nice and behave as we want it.
After adding the java script below:
<script type="text/javascript">
var currentPage = 1;
function showPage(number){
var toShow = document.getElementById('page_' + number);
var toHide = document.getElementById('page_' + currentPage);
toShow.style.display = 'block';
toHide.style.display = 'none';
currentPage = number;}
</script>
And the right references to the div gallery, now all is working. Thanks Bane.

set background color for body depending on if/else statement

I don't know if this is possible but I want to know if I can set the background color of a page depending on an if/else statement?
What I want is that if the if statement is met, then I want the background color of the body to be white, if the else statement is met where it contains a div, then I want the background color of the body to be grey.
Can this be done?
<?php
if (page()){
//all the code in the page
}else{
?>
<div class="boxed">
</div>
<?php
}
?>
UPDATE:
<?php
if (page()){
?>
<body class="bodypage <?php echo page()? "color1":"color2" ?>" >
</body>
<?php
}else{
<div class="boxed">
Continue with Current Assessment
</div>
}
?>
Yes it can be. How about you use a predefined class that have the background color you need:
.color1 {
background-color:red;
}
.color2 {
background-color:yellow;
}
<body class="boxed <?php echo page()? "color1":"color2" ?>" >
</body>
UPDATE:
<body class="bodypage <?php echo page()? "color1":"color2" ?>" >
<?php if (page()){?>
... all the page code
<?php else {?>
<div class="boxed">
Continue with Current Assessment
</div>
?>
A zillion ways to do this.
PHP:
$bodyClass = $condition ? 'foo' : 'bar';
...
echo <<< END_HTML
<body class="$bodyClass">
<!-- stuff here -->
</body>
END_HTML;
CSS:
body.foo { background-color:#fff; }
body.bar { background-color:#888; }
..is one way to do it.
Cheers
Try this
<body <?php if(true) {echo('style="background:red"'); } else { echo('style="background:white"'); } ?> >
</body>
You can do it this way using javascript & jQuery:
<script>
$(document).ready(function(condition){
if (cond) {
$(body).css("background-color","red");
} else {
$(body).css("background-color","blue");
}
});
</script>
Or in php you can just echo that code, with some changes.
Try to minimize the php influence on your design. (CSS for design, html for semantics, php for structure and javascript for interaction)
To accomplish this I recommend sending a hidden input to the browser containing a value:
if(statement) {
$color = '#ffffff';
} else {
$color = '#ff0000';
}
echo '<input id="pageColor" type="hidden" value="'.$color.'" />';
Now do the following with (for example) jquery:
$(document).ready(function() {
//change bg color based on input value
$('body').css('background-color', $('#pageColor').val());
});
Yep, you can do this.
One of the most elegant ways:
PHP part:
<?php
$style = 'background:'; # property
$given = page() ? 'red;' : 'black;'; # Ternary operator
$style .= $given; # Concatenate two strings
?>
HTML part:
<div class="boxed" style='<?php echo $style; ?>'></div>
Also you can create special CSS classes with predefined variety of CSS properties:
PHP part:
<?php
$class = page() ? 'first' : 'second'; # CSS classes defined in your stylesheet.
?>
HTML part:
<div class="boxed <?php echo $class;?>"></div>
And here you go. That's just a quick demonstration, you can replace this with you parameters.

utilize PHP variable in javascript

I just asked another question here: global variable and reduce database accesses in PHP+MySQL
I am using PHP+MySQL. The page accesses to the database and retrieve all the item data, and list them. I was planning to open a new page, but now I want to show a pop div using javascript instead. But I have no idea how to utilize the variables of PHP in the new div. Here is the code:
<html>
</head>
<script type="text/javascript">
function showDiv() {
document.getElementById('infoDiv').style.visibility='visible';
}
function closeDiv() {
document.getElementById('infoDiv').style.visibility='hidden';
}
</script>
</head>
<body>
<ul>
<?php foreach ($iteminfos as $iteminfo): ?>
<li><?php echo($iteminfo['c1']); ?></li>
<?php endforeach;?>
</ul>
<div id="infoDiv" style="visibility: hidden;">
<h1><?php echo($c1) ?></h1>
<p><?php echo($c2) ?></p>
<p>Return</p>
</div>
</body>
</html>
"iteminfos" is the results from database, each $iteminfo has two value $c1 and $c2. In "infoDiv", I want to show the details of the selected item. How to do that?
Thanks for the help!
A further question: if I want to use, for example, $c1 as text, $c2 as img scr, $c1 also as img alt; or $c2 as a href scr, how to do that?
Try this:
<?php foreach ($iteminfos as $iteminfo): ?>
<li>
<a href="javascript:showDiv(<?php echo(json_encode($iteminfo)) ?>)">
<?php echo($iteminfo['c1']); ?>
</a>
</li>
<?php endforeach;?>
Also, modify showDiv to take your row data:
function showDiv(row) {
document.getElementById('infoDiv').style.visibility='visible';
document.getElementById('infoDiv').innerHTML = row['c1'];
}
Basically, you have to consider that the javascript runs in the browser long after the PHP scripts execution ended. Therefore, you have to embed all the data your javascript might need into the website or fetch it at runtime (which would make things slower and more complicated in this case).
Do you want a single info area with multiple items listed on the page and when you click an item the info area is replaced with the new content??? or you want a new info area for each item??
I see something along the lines of the first approach, so I will tackle the latter.
<?php
//do some php magic and get your results
$sql = 'SELECT title, c1, c2 FROM items';
$res = mysql_query($sql);
$html = '';
while($row = mysql_fetch_assoc($res)) {
$html .= '<li><a class="toggleMe" href="#">' . $row['title'] . '</a><ul>';
$html .= '<li>' . $row['c1'] . '</li><li>' . $row['c2'] . '</li></ul>';
$html .= '</li>'; //i like it when line lengths match up with eachother
}
?>
<html>
</head>
<script type="text/javascript">
window.onload = function(){
var els = document.getElementsByClassName("toggleMe");
for(var i = 0, l = els.length; i < l; i++) {
els[i].onclick = function() {
if(this.style.display != 'none') {
this.style.display = 'block';
} else {
this.style.display = 'none';
}
}
}
}
</script>
</head>
<body>
<ul>
<?php
echo $html;
?>
</ul>
</body>
</html>

Categories