i'm using codeigniter.I want to insert data in every iteration to database.
controller
$fee=500;
$trans_fee=300;
$ins_arr=array(2,3);
$ins_array_count=count($ins_arr) ;
if(!in_array('1', $ins_arr))
{
for($i=0;$i<$ins_array_count;$i++)
{
$ins.'_'.$ins_arr[$i]= ($fee+$trans_fee);
$ins_sum+= $ins_.$ins_arr[$i];
}
}
i want to get the data inside this variable like( $ins_2 and $ins_3) and insert the value of $ins_2 and $ins_3 into db
I got value of $ins_sum correctly
Anyone please answer me
Wrap them in {}:
Using ${} is a way to create dynamic variables, simple example:
${'a' . 'b'} = 'hello there';
echo $ab; // hello there
So,
$fee = 500;
$trans_fee = 300;
$ins_arr = array(2,3);
$ins_array_count=count($ins_arr) ;
$ins_sum = 0;
if(!in_array('1', $ins_arr))
{
for($i=0;$i<$ins_array_count;$i++)
{
//$ins_.$ins_arr[$i] = ($fee+$trans_fee);
${"ins_" . $ins_arr[$i]} = ($fee+$trans_fee);
$ins_sum += ${"ins_" . $ins_arr[$i]};
}
}
echo $ins_2; //result of ins_2 = 800
echo "<br />";
echo $ins_3; //result of ins_3 = 800
echo "<br />";
echo $ins_sum; // total ins sum = 1600
Related
I'm learning PHP and i'm trying to show an " €" when and only when $autocollant_total_ht_custom isset.
This is what i wrote :
$euro = " €";
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else echo " ";
However my " €" is always showing even when $autocollant_total_ht_custom is not set.
I spent 75 minutes on it, trying and failing again and again despite researching.
I also tried with !is_null, !is_empty with the same result.
I'm fairly certain that my logic isn't wrong but the way to do it is.
Anyone to the rescue?
Have a nice Saturday everyone !
Mike.
Edit 1:
A little visual aid image
My goal was to only show the content of a cell if there was indeed something in it. By default i could see 0 in the empty cells.
if (!$autocollant_total_ht_lot10) {
$autocollant_total_ht_lot10 = " ";
} else echo "error ";
if (!$autocollant_total_ht_lot20) {
$autocollant_total_ht_lot20 = " ";
} else echo " ";
if (!$autocollant_total_ht_lot50) {
$autocollant_total_ht_lot50 = " ";
} else echo " ";
if (!$autocollant_total_ht_custom) {
$autocollant_total_ht_custom = " ";
} else echo " ";
I know my code must look primitive but it works and i don't see it making a conflict with what we are trying to achieve in the initial question.
Then, as asked, this is what i'm writing in the table row and table data :
<tr>
<td class=table_align_left>A partir de 100</td>
<td><?php echo $autocollant_prix ?></td>
<td><?php echo $autocollant_custom?></td>
<td><?php echo $autocollant_total_ht_custom?> </td>
</tr>
So in short, i'm trying to not show anything if there's no value to be shown (which is currently working) and then adding a " €" after the variable is there's something to be shown.
Edit 2 :
My primitive code : my_code
Edit 3 :
The $autocollant_total_ht_custom is already conditioned to be shown earlier in this statement :
} elseif($autocollant_quantité >= 90 && $autocollant_quantité <= 99){
$autocollant_quantité_lot50 = 2;
} elseif($autocollant_quantité >= 100 && $autocollant_quantité <= 1000){
$autocollant_custom = $autocollant_quantité;
} else echo "entrée invalide";
$autocollant_total_ht_custom = $autocollant_prix * $autocollant_custom;
$autocollant_total_ht_lot10 = $autocollant_prix_lot10 * $autocollant_quantité_lot10;
$autocollant_total_ht_lot20 = $autocollant_prix_lot20 * $autocollant_quantité_lot20;
$autocollant_total_ht_lot50 = $autocollant_prix_lot50 * $autocollant_quantité_lot50;
$pointeuse_total_ht = $pointeuse_prix * $pointeuse_quantité;
$pointeuse_autocollant_offert = $pointeuse_quantité * 10;
$pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
$pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
I posted my code if that can help.
Mike.
//$autocollant_total_ht_custom = null;
$autocollant_total_ht_custom = "something that isnt null";
//if you switch the variable assignment above you will see it behaves as expected.
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo $autocollant_total_ht_custom = $autocollant_total_ht_custom . " " .$euro;
}
else
{
//$autocollant_total_ht_custom wouldn't be set at all if we reach this point, this is why im un-sure what your requirements are. Nothing would be echoed.
echo $autocollant_total_ht_custom;
}
Something like this maybe? It's hard to understand your exact requirements.
IsSet checks if a variable is set to something if its not null then it passes the test, and if you're manipulating strings at this variable then it will never be null, meaning the euro sign will always show up.
If the variable IS null then you fail the conditional test, hit else and echo nothing a null string.
If you can update your answer with what you would expect "$autocollant_total_ht_custom" to be set to, I can help better.
EDIT:
Seems to me you can simplify what you what, basically we are only concerned with echoing a string at all if there is something set, otherwise there's no point doing anything, so your checks could be as simple as
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo 'ht custom';
echo TDFromString($autocollant_total_ht_custom, $euro);
}
//notice this doesnt output anything because it isnt set
if (isset($autocollant_total_ht_lot10, $euro))
{
echo 'lot 10';
echo TDFromString($autocollant_total_ht_lot10, $euro);
}
//notice this does because the string, while empty is something that isnt null
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//lets set it to null and see what happens
$autocollant_total_ht_lot11 = null;
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11 AGAIN';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//it doesnt get printed!
//create a function that takes the string in question,
//and for the sake of your use case also the currency to output,
//that way you could change 'euro' to 'currency'
//and have the sign change based on what the value of the $currency
//string is, eg $currency = "£"
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
Live example : https://3v4l.org/r5pKt
A more explicit example : https://3v4l.org/JtnoF
I added an extra echo to indicate (and newlines) which variable is being printed out you dont need it of course!
I'll just note the function name is a good example of a bad function name, as it not only returns a td around the string but also inserts the currency, you may want to name it a little better :)
EDIT EDIT:
A final edit outside the scope of your question, you should look into keeping your data in arrays and working on them instead.
Using the previous example we can reduce the code to just this !
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_lot12 = "2,0000000";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
//create an array, and stick all our strings in it, from now, if we need to do something to one of the strings(or all!), we do it through the array
$arrayofLots = array($autocollant_total_ht_lot10, $autocollant_total_ht_lot11, $autocollant_total_ht_lot12, $autocollant_total_ht_custom);
//go over each array 'entry' so the first time is '$autocollant_total_ht_lot10', then '$autocollant_total_ht_lot11' etc
foreach ($arrayofLots as $lot)
{
//and we've been over this bit :)
//$lot is a variable we set so we have something to refer to for the individual array entry we are on, we could just as easily name it anything else
if (isset($lot))
{
echo TDFromString($lot, $euro);
}
}
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
Good day. It looks like you are missing the end brace
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else {
echo " ";
}
I am new to this. If I have some PHP code as in the example below, I can use the echo function to print the result. Echo always prints at the top of the screen. How do I format the tag so that in this case the result "$myPi" is printed to the screen using an HTML5 output tag? I am a newbie so please be kind to me and don't flame my post - I tried to format the code. Thanks QJB.
function taylorSeriesPi($Iteration)
{
$count = 0;
$myPi = 0.0;
for ($count=0; ($count<$Iteration);$count++)
{
if ( ($count%4) == 1)
{
$myPi = $myPi + (1/$count);
}
if ( ($count%4) == 3)
{
$myPi = $myPi - (1/$count);
}
}
$myPi *= 4.0;
echo ("Pi is ". $myPi. " After ".$Iteration. " iterations");
}
You can insert PHP anywhere in your document, and reference functions from any other place within the document or included files.
For example:
<?php
function taylorSeriesPi($Iteration)
{
$count = 0;
$myPi = 0.0;
for ($count=0; ($count<$Iteration);$count++)
{
...
}
$myPi *= 4.0;
// Return the value so we can use this function later.
return $myPi;
}
?>
<html>
<body>
<div id="somediv">
<?php
$iteration = 6/*or whatever*/;
echo "Pi is " . taylorSeriesPi($iteration) . " After " . $iteration . " iterations";
?>
</div>
</body>
</html>
This will put the returned value and associated string within the <div> tag, but you can put it anywhere in your HTML, as the output of the echo will simply be text by the time the markup is seen by your browser.
I have written a function which returns a value from a while loop. I call this function 3 times and thus return 3 values. I want to sum these values. My function code is
<?php
function CalcTotal($Acc)
{
require('conn_bal.php');
$query = "SELECT Dte, SUM(Cred), SUM(Deb) FROM tbl_account WHERE Acc='$Acc'";
if ($result=mysqli_query($con,$query)) {
$TotBal = 0;
while($row = mysqli_fetch_array($result)){
global $scred, $sdeb;
$scred=$row['SUM(Cred)'];
$sdeb=$row['SUM(Deb)'];
$bal=$scred-$sdeb;
global $TotBal;
$TotBal = number_format($bal, 2);
return $TotBal;
}
}
}
?>
Which is called 3 different times i.e.
<?php
global $green;
$green = CalcTotal("Green");
echo $green;
echo "<br />";
global $blue;
$blue = CalcTotal("Blue");
echo $blue;
echo "<br />";
global $red;
$red = CalcTotal("Red");
echo $red;
echo "<br />";
?>
I then want to add these 3 returned values i.e.
<?php
global $Total;
$Total = $green + $blue + $red;
echo "Total: $Total";
?>
The correct values are being echoed but $Total = 3 The addition code is giving the count and is not summing the 3 returned variables.
I have tried all the permutations that I know and have researched this problem on the internet but to no avail.
If anyone has any ideas how I might achieve this I would be would be very grateful.
Many Thanks
I am getting data from a MySql database, although I don't know if there could be 1 or more hits from the query.
So, i am echoing something from the database, for example like this:
$code = "
<div style='position:absolute; top:100px'>
'$data'
</div>";
echo $code;
Now, since it is absolute; it is overlapping.
How could I change the 'top:' attribute if there are more results?(For every hit from the database, 'top:' would increment by some amount)
$top = 100; // top default value
$query = mysqli_query($link, 'SQL query is going to be here');
while (null !== ($data = mysqli_fetch_assoc($query)))
{
$code = "
<div style='position:absolute; top:".$top."px'>
".$data."
</div>";
echo $code;
$top += 20; // increment top
}
here's a question : After entering some data about students, i need to print them in top side of the page (form one). I've managed to print data for single student, but i can't make it to store data in $studenti array, so that it will print data for all students.
here's code that i used(i forgot to mention, i need to use sessions for this):
<?php
session_start();
$_SESSION['aindex'] = $_POST['index'];
$_SESSION['aime']= $_POST['ime'];
$_SESSION['aprosek'] = $_POST['prosek'];
//if ($index != "" && $ime != "" && $prosek !="")
//{
// = $index;
//= $ime;
//=$prosek;
//}
//print ($_SESSION['aindex']);
function inicijalizacija()
{
$studenti = array ();
$ind = $_SESSION['aindex'];
$im = $_SESSION['aime'];
$pr = $_SESSION['aprosek'];
$studenti[$ind]["ime"] = $im;
$studenti[$ind]["prosek"] = $pr;
return $studenti;
}
function dodaj($studenti)
{
$studenti[$_SESSION['aindex']]["ime"] = $_SESSION['aime'];
$studenti[$_SESSION['aindex']]["prosek"] = $_SESSION['aprosek'];
return $studenti;
}
function prikazi($studenti) //ovde u argumentu treba $studenti
{
print ("<h2> Lista Studenata: </h2>");
foreach ($studenti as $ind => $student)
{
if (empty($ind))
continue;
$n = $student["ime"];
$p = $student["prosek"];
print ("Index: " . $ind . " " . "Ime: " . $n . " " . "Prosek: " . $p );
}
print("<hr size ='1'>");
//Forma dodavanja
print (" <form action = 'index.php' method = 'post' >");
print ( " Indeks:  <input type = 'text' name = 'index' />");
print(" </br>");
print ( " Ime:       <input type = 'text' name = 'ime' >");
print(" </br>");
print ( " Prosek : <input type = 'text' name = 'prosek' />");
print(" </br>");
print (" <input type = 'submit' value = 'Dodaj' name = 'Dodaj' />");
}
$studenti = inicijalizacija();
?>
<html>
<head> <title> pokusaj </title> </head>
<body>
<?php
prikazi($studenti);
dodaj($studenti);
?>
</body>
</html>
It seems you're misunderstanding the way PHP works. For efficiency and security, all variables are destroyed when the script has ran and the variables used for this user aren't visible for the script when called by other users.
$_SESSION is an exception; data in $_SESSION will be preserved until the session expires, but it will still only be visible to one unique user (identified by a cookie).
If you want to save the data of a script for use when it is called again (using another session), you'll have to write data to a file or use a database.
PS, your script looks like it will introduce XSS and CSRF vulnerabilities; make sure you won't make the same mistakes that many people before you made.