how to get variabel value in other block in php - php

I have a source code like this
<?php
//block1
$a=0;
$b=0;
$c=0;
echo "a".$a."<br>";
echo "b".$a."<br>";
echo "c".$a."<br>";
?>
<?php
//block 2
$a=7;
$b=8;
$c=9;
?>
how to display value of variabel a,b,c in block 2 from block 1?

You can achieve the desired result by controlling the flow of execution using goto, like this:
<?php
//block1
$a = 0;
$b = 0;
$c = 0;
goto location1;
location2:
echo "a = ".$a."<br>";
echo "b = ".$b."<br>";
echo "c = ".$c."<br>";
goto location3;
?>
<?php
location1:
//block 2
$a=7;
$b=8;
$c=9;
goto location2;
location3:
echo "execution continues...";
?>
Output:
a = 7
b = 8
c = 9
execution continues...
Sidenote: Don't use too many goto statements in your code because it'll make your code unreadable for future maintainers.

You will need to create new variables for the second block so that the first blocks variables will keep their values.
Something like this
<?php
//block1
$a=0;
$b=0;
$c=0;
echo "a".$a."<br>";
echo "b".$a."<br>";
echo "c".$a."<br>";
?>
<?php
//block 2
$a2=7;
$b2=8;
$c2=9;
echo "a".$a."<br>";
echo "b".$a."<br>";
echo "c".$a."<br>";
echo "a".$a2."<br>";
echo "b".$a2."<br>";
echo "c".$a2."<br>";
?>

Related

Mixed php and jQuery code for Woocommerce variable products

I am having a problem in adding the product as a variable with variations. I am trying to add accessories on every product page but its working for simple product and is creating the problem in a variable or with variations
i tried to solve this
//this variable code is not working//
<?php if($currentProduct->get_type() == "variable"){ ?>
var varRules = $(".variations_form").data('product_variations');
<?php foreach($currentProduct->get_attributes() as $k=>$v){ ?>
var attribute_<?= getVar($k) ?> = $("select[name=attribute_<?= $k ?>]").val();
<?php } ?>
$.each(varRules,function(){
<?php
$cond = [];
foreach($currentProduct->get_attributes() as $k=>$v){
$cond[] = '(this.attributes["attribute_'.$k. '"]=='. 'attribute_'.getVar($k).' || this.attributes["attribute_'.$k. '"]=="")';
} ?>
if(<?=implode(" && ", $cond)?>){
total = this.display_price;
}
});
//not even this grouped one//
<?php } else if($currentProduct->get_type() == "grouped"){ ?>
var total = 0;
<?php foreach($currentProduct->get_children() as $k=>$id){ ?>
total += $("[name='quantity[<?= $id ?>]']").val() * <?= getPrice(wc_get_product($id)); ?>;
<?php } ?>
<?php } else { ?>
total = parseFloat(<?= getPrice($currentProduct) ?>);
<?php } ?>
i want it to work for variable products
As it seems that you are using php inside some jQuery code, there are some errors like:
Your php opening tags should all be always like <?php but not like <?=
When you want to display something like a variable from php in jQuery, dont forget echo
Dont forget missing ; before your closing php tags.
I have revisited your code, but nobody can test it as it's an extract with missing things and the context is also missing from your question.
<?php if($currentProduct->get_type() == "variable"){ ?>
var varRules = $(".variations_form").data('product_variations');
<?php foreach($currentProduct->get_attributes() as $k => $v){ ?>
var attribute_<?php echo getVar($k); ?> = $("select[name=attribute_<?php echo $k; ?>]").val();
<?php } ?>
$.each(varRules,function(){
<?php
$cond = [];
foreach($currentProduct->get_attributes() as $k=>$v){
$cond[] = '(this.attributes["attribute_'.$k.'"]=='. 'attribute_'.getVar($k).' || this.attributes["attribute_'.$k.'"]=="")';
} ?>
if(<?php echo implode(" && ", $cond) ?>){
total = this.display_price;
}
});
//not even this grouped one//
<?php } else if($currentProduct->get_type() == "grouped"){ ?>
var total = 0;
<?php foreach($currentProduct->get_children() as $k=>$id){ ?>
total += $("[name='quantity[<?php echo $id; ?>]']").val() * <?php echo getPrice(wc_get_product($id)); ?>;
<?php } ?>
<?php } else { ?>
total = parseFloat(<?php echo getPrice($currentProduct); ?>);
<?php } ?>

PHP undefined offset (not an error) try catch

I would like to list (upto) six of the remaining assignments due for the acedemic year onto a webpage.
The data for the remaining assignments comes from a mysql db.
When there are six or more assignments left, the webpage displays the info correctly.
If there are less than six left, then i get a undefined offset error (and i understand why).
Basically, i am trying to programmatically ignore the offset error.
I've tried looking and the following:
try/catches in the php.
tried using (in the foreach loop)
if($row['name'] == null){$row['name'] = "";}
etc.
index.php
include('getdata.php');
$nextAssArray0 = getNextAssessments($courseInfoArray[0], 0);
$nextAssArray1 = getNextAssessments($courseInfoArray[0], 1);
$nextAssArray2 = getNextAssessments($courseInfoArray[0], 2);
$nextAssArray3 = getNextAssessments($courseInfoArray[0], 3);
$nextAssArray4 = getNextAssessments($courseInfoArray[0], 4);
$nextAssArray5 = getNextAssessments($courseInfoArray[0], 5);
<div class="col-sm-4">
<h3>You have <?php echo '' ?> assignments left</h3>
<p>Assignment: <?php echo $nextAssArray0[0]; ?> is due on <?php echo $nextAssArray0[1]; ?></p>
<p>Assignment: <?php echo $nextAssArray1[0]; ?> is due on <?php echo $nextAssArray1[1]; ?></p>
<p>Assignment: <?php echo $nextAssArray2[0]; ?> is due on <?php echo $nextAssArray2[1]; ?></p>
<p>Assignment: <?php echo $nextAssArray3[0]; ?> is due on <?php echo $nextAssArray3[1]; ?></p>
<p>Assignment: <?php echo $nextAssArray4[0]; ?> is due on <?php echo $nextAssArray4[1]; ?></p>
<p>Assignment: <?php echo $nextAssArray5[0]; ?> is due on <?php echo $nextAssArray5[1]; ?>
</p>
</div>
getdata.php
function getNextAssessments($courseID, $offset)
{
try
{
include('dbconn.php');
$array = array();
$stm = $conn->prepare("CALL getUpcomingAssignments(:courseID, :offset)");
$stm->bindParam(':courseID', $courseID);
$stm->bindParam(':offset', $offset);
$stm->execute();
foreach ($stm->fetchALL() as $row)
{
array_push($array, $row['name']);
array_push($array, $row['due_date']);
array_push($array, $row['tem']);
}
}
catch (Exception $e)
{
$array = array('','','');
}
return $array;
}
I am after the following functionality:
1) if there is only three assignments left - then only three display.
2) if there is ten assignments left - then only six are displayed.
3) if there are no assignments left - then nothing is displayed in those
First of all, it is not a good idea to make SQL query for every database record. Remove limit from query, and put all your assesments in array. Then do something like this:
<h3>You have <?php echo count($assesments) ?> assignments left</h3>
<?php
foreach($assesments as $assesment){
echo "<p>Assignment: {$assesment[0]} is due on {$assesment[1]} ?></p>"
}
?>
Also I would recommend changing your code so you have array like $assesment['dueDate'] instead of $assesment[1].

Dynamic variable in PHP [GET]

I'm trying to use the php get. everthing is working find exept the include page.
it's adding 1 at the end of include content
any ide how do i ride that?
example:
I love norway
I love norway1
Code:
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') $norge = include('norway.php');
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
We should use file_get_contents, instead of include.
$norge = file_get_contents("norway.php");
Use the output buffer functions to capture the output from the included script.
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') {
ob_start();
include("norway.php");
$norge = ob_get_clean();
}
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>

How to display the php values into html table?

I have almost did the displaying of values.But i am getting an extra td I hope the complete code is right.
Can anyone see it once.
php
<?php
session_start();
$link = mysqli_connect('localhost','root','','hoteldetails');
$sno[]="";
$roomImage[]="";
$roomNo[] = "";
$hotelName[]="";
$roomPrice[]="";
$loc[]="";
if(isset($_POST['sub']))
{
// mysqli_s(elect_db($link, "hotels");
$location=$_POST['searchVal'];
$sql = "select * from roomdetails where Location = '$location'";
$sqldata= mysqli_query($link ,$sql);
while($row = mysqli_fetch_array($sqldata)){
$sno[]=$row['S.No'];
$roomImage[] = $row['RoomImage'];
$roomNo[] = $row['RoomNo'];
$hotelName[] = $row['HotelName'];
$roomPrice[] = $row['RoomPrice'];
$loc[] = $row['Location'];
}
//two arrays to display ,$combine = array_combine($one,$two);
}
?>
html
<?php
echo "<table border='1'>";
echo "<tr><th>Hotel</th><th>Location</th></tr>";
foreach($sno as $id => $key):
echo "<tr>";
echo "<td>";?><img src="<?php echo $roomImage[$id];?>" height="100" width="100"><?php echo "</td>";
echo "<td>".$roomNo[$id]."</td>";
echo "<td>".$hotelName[$id]."</td>";
echo "<td>".$roomPrice[$id]."</td>";
echo "<td>".$loc[$id]."</td>";
echo "</tr>";
endforeach;
echo "</table>";
//echo $html;
?>
Is there anything wrong in it . Please is this process is right to display the values from mysql into html.
First, format your code, using tools like http://phpbeautifier.com for example.
Next, you have lines that contain both an echo and a ?> ... <?php instruction that tells php to echo stuff without any PHP parsing.
This is quite unreadable and unmaintainable.
You should better go for some "templating-like" practices using only HTML and using PHP only to output some vars, like this:
<?php
foreach ($data as $item) {
?>
<tr>
<td>Name: <?php echo $item['name']; ?></td>
<td>Price: <?php echo $item['price']; ?></td>
</tr>
<?php
}
This would drastically improve readability, maintainability, and especially your capacity of debugging the code.
And finally, you should take care of your php.ini configuration and check that display_errors is set to 1 and error_reporting is set to E_ALL.
This way, any error like PHP - Notice : Undefined index ... will be shown in the output so you can debug it.

How to speedup by code?

is there a way to speed up my code? It takes about 15 seconds to load ... I don't really see a way to reduce my code... I just thought about inserting the values into database, so the user does not have to load new info every time.. but the thing is that my cron only allows 1 load per hour ... by loading new info on every load it gives me fresh information..
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
As I wrote in the comment, fetch the page once instead of once for every name in the database.
Pseudo code for my comment:
users = <get users from database>
webpage = <get webpage contents>
for (user in users)
<check if user exists in webpage>
As mentioned in the comments you're calling a webpage for each entry in your database, assuming that's about 2 seconds per call it's going to slow you down a lot.
Why don't you call the page once and pass the contents of it into the check_online() function as a parameter so your code would look something like this which will speed it up by quite a few magnitudes:
$content=file_get_contents("http://www.tibia.com/community/?subtopic=worlds&worl‌​d=Isara",0);
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named,$content);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
and your check_online() function would look something like this:
function check_online($name,$content){
$count=substr_count($name, " ");
if($count > 0){ $ex=explode(" ",$name); $namez=$ex[1]; $nameused=$namez; }
else{ $nameused=$name; }
if(preg_match("/$nameused/",$content)){ $status="online"; }
else{ $status="offline"; }
return $status;
}
You can also do the following to make it faster
Stop using select * which is very bad on innodb
Put better indexes on your database to make the recordset return faster
Install PHP 5.4 as it's faster especially as you're creating a new object in each iteration
Use a byte code accelerator/cache such as xdebug
you should avoid using distinct (*) keyword in your SQL Query
for more information read this http://blog.sqlauthority.com/category/sql-coding-standards/page/2/

Categories