I have this:
$nome='x.png';
if(file_exists('../../images/produtos/'.$nome)){
$i = 1;
while(file_exists('../../images/produtos/'.$i."_".$nome))
{
$i++;
}
$nome = $i."_".$nome;
}
and then i need to put the variable $nome into a mysql_query, just like this
mysql_query("INSERT INTO produtos (nome) VALUES ('$nome)");
but it doesnt show the 'edited' variable.
As noted in the answer to the following Stackoverflow post by #PhpXp, you could try to define the variable before the while loop. See below:
$report = "";
while ($row = mysql_fetch_array($result)) {
$report += "a"."b".$row["prevDOCid"]+1;
}
echo $report;
Related
$result = mysqli_query($con, "SELECT * FROM users");
$usersArray=[];
tableArrayPushData($result, $usersArray);
function tableArrayPushData($result, $tableArray){
while ($row = $result->fetch_assoc()) {
$str = '';
foreach ($row as $value) {
$str = $str.$value.'|';
}
$newStr = rtrim($str, "| ");
array_push($tableArray,$newStr);
}
}
for ($i=0; $i<count($usersArray); $i++){//array is always empty at this point
echo "Ok";
echo "<br>";
}
I don't understand why, but usersArray is empty despite the fact that I added data there.
The MySQL table has rows with data, so it can't be empty.
You should use the & operator to allow the function to access the outer variable, like this:
function tableArrayPushData($result, &$tableArray) {}
Or use return.
i have a little bit problem with my following code. I am trying to make a star raiting system and following code is showing raiting details. But i am getting
undefined variable rate_db notice
from if(count($rate_db)) What i missing here anyone can tell me please ?
full code is here:
<?php
$query = mysqli_query($db,"SELECT ryid,r_type FROM profileRaiting WHERE rp_uid='$profile_uid'");
while($data = mysqli_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['r_type'];
}
if(count($rate_db)){
$rate_times = count($rate_db);
$sum_rates = array_sum($sum_rates);
$rate_value = $sum_rates/$rate_times;
$rate_bg = (($rate_value)/5)*100;
} else {
$rate_times = 0;
$rate_value = 0;
$rate_bg = 0;
}
?>
It's better to initialize your both arrays at top as:
$rate_db = array();
$sum_rates = array();
And i don't know where you define this variable $profile_uid, if it's user input or session value, than your code is open for SQL Injection, you can use Prepared Statement to prevent SQL attack. And this post will help you to understand: How can I prevent SQL injection in PHP?
Side Note & Suggestion:
One more thing, you can also get the sum of r_type as:
$sum_rates += $data['r_type'];
In this case, you need to initialize $sum_rates as:
$sum_rates = 0;
And just remove this line from IF condition:
$sum_rates = array_sum($sum_rates);
Benefit, no need to use extra method.
$rate_db variable is defined in the while loop where you fetch the data from MySQL. However, if the query does not return any records, then while loop's body will not be executed, thus $rate_db variable will not be defined.
Solution: define $rate_db variable as an empty array before the while loop.
You can initialize the variable:
<?php
$rate_db = array();
$sum_rates = array();
$query = mysqli_query($db,"SELECT ryid,r_type FROM profileRaiting WHERE rp_uid='$profile_uid'");
while($data = mysqli_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['r_type'];
}
$rate_times = count($rate_db);
if($rate_times > 0){
$sum_rates = array_sum($sum_rates);
$rate_value = $sum_rates/$rate_times;
$rate_bg = (($rate_value)/5)*100;
} else {
$rate_times = 0;
$rate_value = 0;
$rate_bg = 0;
}
?>
before using count you need to be sure that it does have something in it so your complete code can be like this
<?php
$query = mysqli_query($db,"SELECT ryid,r_type FROM profileRaiting WHERE rp_uid='$profile_uid'");
while($data = mysqli_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['r_type'];
}
if(isset($rate_db) && count($rate_db)){
$rate_times = count($rate_db);
$sum_rates = array_sum($sum_rates);
$rate_value = $sum_rates/$rate_times;
$rate_bg = (($rate_value)/5)*100;
} else {
$rate_times = 0;
$rate_value = 0;
$rate_bg = 0;
}
?>
$query = mysqli_query($db,"SELECT ryid,r_type FROM profileRaiting WHERE rp_uid='$profile_uid'");
while($data = mysqli_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['r_type'];
}
Because when your query returns empty result the while loop will never become true, so $rate_db[] will never be reached and therefore will never be created.
to fix this, you should declare $rate_db[] as empty first at the top of your script. to avoid the error.
I have a variable:
$category = 3;
$data = MY SQL QUERY....
while ($data2 = mysql_fetch_array( $data))
{ echo $data2['$category'] ; }
I want to get the sql value of echo $data2['3']; by using the vairable $category ...
is there a way to do this ?
Your code looks correct but you have to remove the single quotes
{ echo $data2['$category'] ; }
should be changed to
{ echo $data2[$category] ; }
I currently have 2 arrays where i would like to compare dates in. here are how my arrays are structured:
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');
The aim is to detect whether or not a value from $userdaysoff exists in the $bholidays array.
The above works great and detects that 26-05-2014 exists in both arrays, but if the $userdaysoff array looks like this:
$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');
Then the duplicate date 26-05-2014 is not detected.
Is there any reason why this would be occuring?
here is how i run my code:
$results = array_intersect($bholidays, $userdaysoff);
if($results){
foreach($results as $result){
echo 'yes';
}
} else {
echo 'no';
}
Could you not quite simply use in_array?
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');
$count = count($userdaysoff);
for($i = 0; $i == $count; $i++) {
if(in_array($userdaysoff[$i], $bholidays)) {
echo $userdaysoff[$i] . " is in array.";
}
}
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');
$results = array_intersect($bholidays, $userdaysoff);
if($results)
{
foreach($results as $result)
{
echo 'yes';
}
}
else
{
echo 'no';
}
Run this code and check it works fine..
The output is yes.
i searched all over the web or i just don't see it.
I want to do the following:
<?php
function abc(){
$sql = "SELECT * FROM table";
$result = mysql_fetch_assoc($sql);
$data = mysql_fetch_assoc($result);
}
?>
<? while(abc()) : ?>
<?=article_title()?>
<?=article_content()?>
<?=endwhile;?>
Could somebody give me a direction and/or example?
Thank you very much
PHP does not have generator/iterator functions, so you cannot do that.
You can however return an array and iterate over that array:
function abc() {
// ...
$rows = array();
while($row = mysql_fetch_assoc($result)) { $rows[] = $row; }
return $rows;
}
foreach(abc() as $row) {
// do something
}
If the functions you call need access to the row, pass it as an argument. Using global variables for it would be very bad/nasty
have you tried to do something like that?
<?php
function getData(){
//get the data from the database
//return an array where each component is an element found
}
$elems = getData();
foreach($elems as $e){
doSomethingWith($e);
}