I have the following php code:
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
return $output;
For some reason though, the value of $skizzar_double_width_size is not being added into the $output - is there a way to echo a value in an output variable?
As #Rizier123 mentioned, ensure you initialise any string variables before trying to append to them.
$var = '';
$var .= 'I appended';
$var .= ' a string!';
I would also like to strongly discourage you from using inline styles as well as generating them with inline PHP. Things get very messy very quickly.
In a situation like this you need to check that all the variables you are using in the calculation are valid before you panic.
So try
echo 'before I use these values they contain<br>';
echo '$masonry_item_width = ' . $masonry_item_width . '<br>';
echo '$masonry_item_padding = ' . $masonry_item_padding . '<br>';
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
echo 'after moving the fields to an unnecessary intemediary field<br>';
echo '$skizzar_masonry_item_width = ' . $skizzar_masonry_item_width . '<br>';
echo '$skizzar_masonry_item_padding = ' . $skizzar_masonry_item_padding . '<br>';
echo '$skizzar_double_width_size = ' . $skizzar_double_width_size . '<br>';
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
echo $output;
This should identify which fields are causing you problems.
Also while testing always run with display_errors = On It saves so much time in the long run.
Related
I want to store an property->object name in a table and access in code.
What is the proper way to accomplish the following.
$patient = new stdClass();
$patient->patient_number = '12345';
$cls = 'patient';
$prp = 'patient_number';
echo 'test1 = ' . $patient->patient_number . '<br>';
//--- this prints 12345 as expected;
echo 'test2 = ' . $$cls->patient_number . '<br>';
//--- this print 12345 as expected;
echo 'test3 = ' . $$cls->${$prp} . '<br>';
//--- this generates undefined variable patient_number;
Use this:
echo 'test3 = ' . ${$cls}->{$prp} . '<br>';
I'm trying to use Simple_Html_Dom parser to parse a web page for NBA statistics. I'm going to have a lot of different urls, but parsing the same data, so I figured I would create a function. Outside of the function, this parser works great, however as soon as I place the parsing inside the function, I receive a connection error. Just wondering, if anyone knows why I can't run the file_get_html inside the function. Here is the code below. Please help!
include('simple_html_dom.php');
$nbaPlayers = 'playerstats/15/1/eff/1-2';
function nbaStats($url){
$html = 'http://www.hoopsstats.com/basketball/fantasy/nba/';
$getHtml = $html . $url;
$a = file_get_html("$getHtml");
foreach($a->find('.statscontent tbody tr') as $tr){
$nbaStatLine = $tr->find('td');
$nbaName = $nbaStatLine[1]->plaintext;
$nbaGamesPlayed = $nbaStatLine[2]->plaintext;
$nbaMinuesPlayed = $nbaStatLine[3]->plaintext;
$nbaTotalPoints = $nbaStatLine[4]->plaintext;
$nbaRebounds = $nbaStatLine[5]->plaintext;
$nbaAssists = $nbaStatLine[6]->plaintext;
$nbaSteals = $nbaStatLine[7]->plaintext;
$nbaBlocks = $nbaStatLine[8]->plaintext;
$nbaTurnovers = $nbaStatLine[9]->plaintext;
$nbaoRebounds = $nbaStatLine[11];
$nbadRebounds = $nbaStatLine[12];
$nbaFieldGoals = $nbaStatLine[13];
$nbaFieldGoals = explode("-", $nbaFieldGoals);
$nbaFieldGoalsMade = $nbaFieldGoals[0];
$nbaFieldGoalsAttempted = $nbaFieldGoals[1];
// Player Stat Line
$playerStats = $nbaName . ': gp - ' . $nbaGamesPlayed . ' mp - ' . $nbaMinutesPlayed . ' pts - ' . $nbaTotalPoints . ' rb - ' . $nbaRebounds . ' as - ' . $nbaAssists . ' s - ' . $nbaSteals . ' bl - ' . $nbaBlocks . ' to - ' . $nbaTurnovers;
echo $playerStats . '<br /><br />';
}
}
nbaStats($nbaPlayers);
When passing a variable to a function you don't need to wrap the variable in quotations. Quotations are used when passing text directly to a function or assigning text to a variable.
Change this:
$a = file_get_html("$getHtml");
To this:
$a = file_get_html($getHtml);
First and foremost, forgive me if my language is off - I'm still learning how to both speak and write in programming languages. How I can retrieve an entire object from an array in PHP when that array has several key, value pairs?
<?php
$quotes = array();
$quotes[0] = array(
"quote" => "This is a great quote",
"attribution" => "Benjamin Franklin"
);
$quotes[1] = array(
"quote" => "This here is a really good quote",
"attribution" => "Theodore Roosevelt"
);
function get_random_quote($quote_id, $quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
} ?>
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
Using array_rand and var_dump I'm able to view the item in the browser in raw form, but I'm unable to actually figure out how to get each element to display in HTML.
$quote = $quotes;
$random_quote = array_rand($quote);
var_dump($quote[$random_quote]);
Thanks in advance for any help!
No need for that hefty function
$random=$quotes[array_rand($quotes)];
echo $random["quote"];
echo $random["attribution"];
Also, this is useless
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
If you have to run a loop over all the elements then why randomize hem in the first place? This is circular. You should just run the loop as many number of times as the quotes you need in output. If you however just need all the quotes but in a random order then that can simply be done in one line.
shuffle($quotes); // this will randomize your quotes order for loop
foreach($quotes as $qoute)
{
echo $quote["quote"];
echo $quote["attribution"];
}
This will also make sure that your quotes are not repeated, whereas your own solution and the other suggestions will still repeat your quotes randomly for any reasonably sized array of quotes.
A simpler version of your function would be
function get_random_quote(&$quotes)
{
$quote=$quotes[array_rand($quotes)];
return <<<HTML
<h1>{$quote["quote"]}</h1>
<p>{$quote["attribution"]}</p>
HTML;
}
function should be like this
function get_random_quote($quote_id, $quote) {
$m = 0;
$n = sizeof($quote)-1;
$i= rand($m, $n);
$output = "";
$output = '<h1>' . $quote[$i]["quote"] . '.</h1>';
$output .= '<p>' . $quote[$i]["attribution"] . '</p>';
return $output;
}
However you are not using your first parameter-$quote_id in the function. you can remove it. and call function with single parameter that is array $quote
Why don't you try this:
$quote = $quotes;
$random_quote = array_rand($quote);
$random = $quote[$random_quote];
echo '<h1>' . $random["quote"] . '.</h1><br>';
echo '<p>' . $random["attribution"] . '</p>';
Want to create a function:
echo get_random_quote($quotes);
function get_random_quote($quotes) {
$quote = $quotes;
$random_quote = array_rand($quote);
$random = $quote[$random_quote];
return '<h1>' . $random["quote"] . '.</h1><br>'.'<p>' . $random["attribution"] . '</p>';
}
First, you dont need the $quote_id in get_random_quote(), should be like this:
function get_random_quote($quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
}
And I cant see anything random that the function is doing. You are just iterating through the array:
foreach($quotes as $quote_id => $quote) {
echo get_random_quote( $quote);
}
According to http://php.net/manual/en/function.array-rand.php:
array_rand() Picks one or more random entries out of an array, and
returns the key (or keys) of the random entries.
So I guess $quote[$random_quote] should return your element, you can use it like:
$random_quote = array_rand($quotes);
echo get_random_quote($quote[$random_quote]);
I'm using PHPQuery to read some content from HTML, I'm unable to get the element by it's index using the square bracket notation.
See this simple example:
$html = '<div><table id="theTable"><tr><td>FIRST TD</td><td>SECOND TD</td><td>THIRD TD</td></tr></table></div>';
$pq = phpQuery::newDocumentHTML($html);
$table = $pq->find('#theTable');
$tds = $table->find('td');
echo "GETTING BY INDEX:\n\n";
echo '$tds[1] = ' . $tds[1];
echo "\n\n\n";
echo "GETTING IN FOREACH:\n\n";
foreach($tds as $key => $td){
echo '$tds[' . $key . '] = ' . pq($td) . "\n";
}
The output of this is:
GETTING BY INDEX:
$tds[1] =
GETTING IN FOREACH:
$tds[0] = FIRST TD
$tds[1] = SECOND TD
$tds[2] = THIRD TD
I would have expected that I can get the contents of $tds[1] using square brackets, but seems not. How can I get it by index?
Try a var_dump($tds), it'll tell you whats exactly inside the tds. Maybe those keys are actually strings and you should use:
echo "GETTING BY INDEX:\n\n";
echo '$tds['1'] = ' . $tds['1'];
Edit: Also, on your foreach you're using pq(), maybe you should use this
echo "GETTING BY INDEX:\n\n";
echo '$tds[1] = ' . pq($tds[1]);
Found the answer just after posting the question. Instead of square brackets you need to use eq(n):
echo '$tds[1] = ' . $tds->eq(1);
Try the following:
echo '$tds[1] = ' . $tds['1'];
So I am using Codeignitor and I am trying to figure out the best way to share my constants with my javascript in a neat maintainable way.
1) in the view I could echo out my variables in like my footer (yuuuck!)
2) I could parse a partial view which contains a template for javascript and inject that in my view (maybe?)
3) I could dynamically create a javascript file like myJavascript.js.php and include that in my header.
What's the best maintainable way to implement PHP into JS in a MVC framework?
To keep my variables nicely wrapped I use a JSON object - that way I won't incur in issues with encoding, slashes, having to manually update the JavaScript every variable I add...
$variables_to_view['js_variables']['var_name'] = $var_name;
then pass it to the view
php_variables = <?php echo json_encode($js_variables) ?>;
alert(php_variables.var_name);
There doesn't seem to be anything wrong about echoing your variables in the script tag. In fact, frameworks like BackboneJS are encouraging you to do so for data you need to pass to your client-side code.
You can use short tag like this:
For Example:
You want to use $abc variable in js, then you will need to write this in js
var abc = <?=$abc?>;
You can create php file .
Something like script.js.php?outfor=1;
<?php
header("Content-type:text/javascript"); //can be application/javascript.
?>
ABC = <?php echo $abc?>
CBA = <?php echo $cba?>
BAC = <?php echo $bac?> //and so on.
Some additional info .
If you use "var" in function that variable will be visible only in that function and without "var"means global.
So.
function abc()
{
var a = 1; //only in abc()
b=2; //global
}
I know that in terms of programming skills it's not the best, but finally it's what I use and it's working. To make it short: I put all my constants in a xml file and I have this little script that generates two separate files with the same content, but different syntax. I am just pasting the code with my values. If it's useful for anybody, I'll be very happy to help.
The xml is the simplest possible; value
<?php
define("GECOXML_PATH","../xml/geco.xml");
define("PHP_GECO_FN","../.includes/geco.php");
define("JS_GECO_FN","../js/geco.js");
echo "******** GECO (GEnerate COnstants files for PHP and JS) **********<br>";
echo "<br>";
echo " input xml file: ". GECOXML_PATH."<br>";
echo " output php file: ". PHP_GECO_FN."<br>";
echo " output js file: ". JS_GECO_FN."<br>";
echo "********************************************************************<br>";
$geco = (object)xmlParse(GECOXML_PATH);
echo "<br>";
echo "<br>";
echo "************ PHP GECO ************* <br>";
echo "<br>";
$PHP = gecoPHP($geco);
echo "<br>";
echo "<br>";
echo "************** JS GECO ************<br>";
echo "<br>";
$JS = gecoJS($geco);
writeFiles($PHP, $JS);
//****** Functions *********
function xmlParse ($url) {
$fileContents= file_get_contents($url);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
return simplexml_load_string($fileContents);
}
function writeFiles($PHPcontent, $JScontent)
{
echo "<br> PhP ok:". file_put_contents(PHP_GECO_FN, $PHPcontent) . "<br>";
echo "<br> JS ok:" . file_put_contents(JS_GECO_FN, $JScontent) . "<br>";
}
function gecoPHP($gecoOBJ)
{
foreach ($gecoOBJ as $key => $value)
{
if (is_numeric(str_replace(" ","",$value)))
{
$line = "define(\"" . $key . "\",". intval($value) . ");\n";
}
else
{
$line = "define(\"" . $key . "\",\"". $value . "\");\n";
}
$phpContent = $phpContent . $line;
echo $line."<br>";
}
return "<?php\n"$phpContent."?>";
}
function gecoJS($gecoOBJ)
{
foreach ($gecoOBJ as $key => $value)
{
if (is_numeric(str_replace(" ","",$value)))
{
$line = "var " . $key . "=". $value . ";\n";
}
else
{
$line = "var " . $key . "=\"". $value . "\";\n";
}
$JSContent = $JSContent . $line;
echo $line."<br>";
}
return $JSContent;
}
?>