So i have this problem i can't figure out. but what the problem is that when i press the right arrow in the page, the value has to go up by 1, not 2. and when pressed the left button, it should go -1 and that works normally.
i try to get all the id's from the database and put it in a array. the functions check the array for end and first values and put's the data to the page input.
here is the code where i struggle with:
// checks if the variable has not been set, otherwise errors and problems will occur
if (empty($_SESSION["huidigeklant"])) {setHuidigeKlant();}
// common variables set to use
$id[] = $_SESSION["id"];
// makes a pre connection to check if a value can be set up or down to prevent errors in the input page.
$result = mysqli_query($conn, "SELECT * from klanten"); // run the query and assign the result to $result
while ($row=mysqli_fetch_assoc($result)) {
array_push($id, $row["id"]);
}
// checks first and last array value of the session id
function checkfirstklant(){ $firstID = reset($id); return $firstID; }
function checklastklant(){ $lastID = end($id); return $lastID; }
// checks first if the right or left button has been pressed and checks after that if the value is already at the first or end array
if (isset($_POST['leftbutton'])){
if (($firstID = checkfirstklant()) != $_SESSION["huidigeklant"]) {
$_SESSION['huidigeklant']--;
echo "huidige klant = " . $_SESSION["huidigeklant"];
}
}
if (isset($_POST['rightbutton'])){
if (($lastID = checklastklant()) != $_SESSION["huidigeklant"]) {
$_SESSION['huidigeklant']++;
echo "huidige klant = " . $_SESSION["huidigeklant"];
}
}
one thing to mention and already figured that out. the "setHuidigeKlant() sets $_SESSION["huidigeklant"] to 1".
so my question is: How can i prevent the program to set the value to 1, but still set the value to 1 if it hasn't been set in the first place?
i coudln't find a specific answer for this, but if you do. send a link to it please
thanks in advance ;)
So i already found my problem xD
what Difster already said at the post above, i need to isset my $_SESSSION["huidigeklant"] instead of checking if it is empty.
and i have to check if i am already at the end of the variable in the if statement instead of letting that doing by a function.
so now everything works as requested ;)
thanks again
Try this:
if (!isset($_SESSION["huidigeklant"])) { $_SESSION["huidigeklant"] = 1;}
so I want to make a notification with AJAX call, that checks the sum of rows in database, then compare it the amount from before.
when the AJAX access the php file, first it will check if $old_db is already defined or not, if not then define it.
then it will run the query(not using prepared statement yet because this still experimental to me) to check, num_row query and the store the value into $new_db.
after that it will compare the value and goes on.....
then finally it will assign $new_db value to $old_db
if(!(isset($old_db)) && empty($old_db)){
$old_db = "";
}
$sql = $con->query("SELECT * FROM produk");
$new_db = $sql->num_rows;
if($new_db > $old_db){
echo "ada";
echo "<br>".$old_db;
}
$old_db= $new_db;
now the problem whenever I run the php file, it never echoed the value of $old_db even though I already assign a value to it at the bottom of the script
I guess everytime I run the script the value got assigned as "" or null again? so how do I prevent that and keep the last value?
I'm thinking about storing it into Cookie....
after thinking about it if I only want to use it as notification then using Session is enough. thank you guys
session_start();
$_SESSION['old_db'] = $old_db;
if(!(isset($_SESSION['old_db']) && empty($_SESSION['old_db'])){
$_SESSION['old_db'] = "";
}
$sql = $con->query("SELECT * FROM produk");
$new_db = $sql->num_rows;
if($new_db > $_SESSION['old_db']){
echo "ada";
echo "<br>".$_SESSION['old_db'];
}
$_SESSION['old_db']= $new_db;
In this script, I basically want to echo out data from a mysql table when the certain row record matches the name in browser address bar. But for some reason, no data is echoed. Any reason my while loop doesn't work?
$users = $_GET['username'];
$other_friend_query = mysql_query("SELECT * FROM friend WHERE RelatingUser='$users'");
$other_friend_assoc = mysql_fetch_assoc($other_friend_query);
while ($other_friend_assoc = mysql_fetch_assoc($other_friend_query)) {
$friend_related = $other_friend_assoc['RelatedUser'];
echo $friend_related;
}
That's because you call mysql_fetch_assoc before your while loop. When you call this, it fetches a result. Then when you go to your loop, that result has already been fetched. Just remove that and you'll be good to go. As stated in the comments, stop using mysql_ functions. The code below will escape your posted variable and also check for errors. It's also better practice to use a column list, instead of SELECT *.
$users = mysql_real_escape_string($_GET['username']);
$other_friend_query = mysql_query("SELECT * FROM friend WHERE RelatingUser='$users'") or die( mysql_error() );
while( $other_friend_assoc = mysql_fetch_assoc($other_friend_query) ) {
$friend_related = $other_friend_assoc['RelatedUser'];
echo $friend_related;
}
Any reason my while loop doesn't work?
May not be the only reason, but you're skipping the first result.
// the following line is unused and therefore unnecessary
$other_friend_assoc = mysql_fetch_assoc($other_friend_query);
while ($other_friend_assoc = mysql_fetch_assoc($other_friend_query)) {
Hello everyone this is my first post on stack overflow.com
I am trying to create shopping cart. Values are being stored in a session in two dimension table. First value represents item id in database, second - quantity of item.
foreach($cart_array as $row){
$sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
$cart_result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($cart_result)) {
echo "<tr><td>". $array[manufacturer] . "</td>";
echo "<td>". $array[model]."</td>";
echo "<td>".$row['1']."</td>";
$cart_value = $array[price]*$row['1'];
//sum and store value of all products
$total_cart_value += $cart_value;
echo "<td>£" .$cart_value. "</td>";
echo "<td><a href='search.php?kick_id=".$row['0']."'>Remove</a></td></tr>";
OK, to remove item form cart user clicks remove and then he is being send back to the same page ( for some reason I had difficulty to use $_SERVER['PHP_SELF'} with GET method... )
and then different part of code is being triggered which is going to remove pair of values from $cart_array, using unset function.
if(isset($_GET['kick_id'])) {
$t = count($cart_array);
for( $u = 0; $u < $t; $u++){
if ($cart_array[$u]['0'] == $_GET['kick_id']){
unset($cart_array[$u]['0']);
unset($cart_array[$u]['1']);
echo " Item has been removed from your cart";
$cart_array = array_values($cart_array);
And it actually removes pair of values but, now instead of running smoothly it displays error message :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource ...
I assume that there must be some "trace" of deleted values in session array which is causing error.
How to fix code ?
Is there any other method/approach to delete values ??
You don't appear to have unset($cart_array[$u]), just the two keys it contains. Don't bother unsetting $cart_array[$u]['0'] and '1', just unset $cart_array[$u].
I assume that...
that is what you are doing wrong.
A programmer should never "assume". A programmer can (and should) be certain.
Assumptions will lead you astray and waste your time (as well as other people's time when you ask a question).
So, you have to do some debugging - an investigation to find the actual cause of the problem.
supplied argument is not a valid MySQL result resource error means there was an error in the query. So, you have to see what error it was. So, run all yoiur queries in the manner which let you to see any error occurred. A simplest way would be
$cart_result = mysql_query($sql, $conn) or trigger_error(mysql_error()." in ".$sql);
and run your code again.
you will find an sql error in their usual place - a screen or a log file.
If you'll be unable to get the meaning of the error and correct your code yourself - it's time to ask a question. A certain and direct question, not vague question out of some assumptions. Gotcha?
There are two ways to solve this:
1) on your first part:
foreach($cart_array as $row){
if(empty($row[0])
continue;
$sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
[...]
2) when you remove the item, instead of unset($cart_array[$u]['0']); why don't you do unset($cart_array[$u]); ?
Probably this
while ($array = mysql_fetch_array($cart_result)) {
is causing the problem. based on error you see. before you try fetch result check if there is any results available by
if(mysql_num_rows($cart_result)>0){
while ($array = mysql_fetch_array($cart_result)) {
//rest code here
}
I am getting this PHP error, what does it mean?
Notice: Undefined offset: 0 in
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
From this code:
<?php
include("config.php");
function getAllVotes($id)
{
$votes = array();
$q = "SELECT * FROM entries WHERE id = $id";
$r = mysql_query($q);
if(mysql_num_rows($r)==1)//id found in the table
{
$row = mysql_fetch_assoc($r);
$votes[0] = $row['votes_up'];
$votes[1] = $row['votes_down'];
}
return $votes;
}
function getEffectiveVotes($id)
{
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1]; //ERROR THROWN HERE
return $effectiveVote;
}
$id = $_POST['id'];
$action = $_POST['action'];
//get the current votes
$cur_votes = getAllVotes($id);
//ok, now update the votes
if($action=='vote_up') //voting up
{
$votes_up = $cur_votes[0]+1; //AND ERROR THROWN HERE
$q = "UPDATE threads SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down')
{
$votes_down = $cur_votes[1]+1;
$q = "UPDATE threads SET votes_down = $votes_down WHERE id = $id";
}
$r = mysql_query($q);
if($r)
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote." votes";
}
elseif(!$r) //voting failed
{
echo "Failed!";
}
?>
You are asking for the value at key 0 of $votes. It is an array that does not contain that key.
The array $votes is not set, so when PHP is trying to access the key 0 of the array, it encounters an undefined offset for [0] and [1] and throws the error.
If you have an array:
$votes = array('1','2','3');
We can now access:
$votes[0];
$votes[1];
$votes[2];
If we try and access:
$votes[3];
We will get the error "Notice: Undefined offset: 3"
first, check that the array actually exists, you could try something like
if (isset($votes)) {
// Do bad things to the votes array
}
This answer helped me https://stackoverflow.com/a/18880670/1821607
The reason of crush — index 0 wasn't set. Simple $array = $array + array(null) did the trick. Or you should check whether array element on index 0 is set via isset($array[0]). The second variant is the best approach for me.
Use print_r($votes); to inspect the array $votes, you will see that key 0 does not exist there. It will return NULL and throw that error.
getAllVotes() isn't returning an array with the indexes 0 or 1. Make sure it's returning the data you want by calling var_dump() on the result.
I encountered this as well and the solution is simple, dont hardcode the array index position in your code.
Instead of
$data[0]['somekey'] do
foreach($data as $data_item)
{
echo $data_item['somekey'];
} If there is an array or more you can perform your desired action inside the loop, but if it's undefined it will not bring an error. you can also add other checks on top of that.
You could also add a variable and increment it in a for in loop to limit your looping if you want only the first positions or something.
As explained it happens because there is no data in the $cur_votes[0] and hence it throws an error.
To ensure your code works fine, before performing "$votes_up = $cur_votes[0]+1;" echo the $cur_votes[0] value to see if there is any value stored or not.
Surely, there is no value stored.
function getEffectiveVotes($id)
According to the function header, there is only one parameter variable ($id).
Thus, on line 27, the votes[] array is undefined and out of scope. You need to add another
parameter value to the function header so that function getEffectiveVotes() knows to expect two parameters. I'm rusty, but something like this would work.
function getEffectiveVotes($id, $votes)
I'm not saying this is how it should be done, but you might want to research how PHP
passes its arrays and decide if you need to explicitly state to pass it by reference
function getEffectiveVotes($id &$votes) <---I forget, no time to look it up right now.
Lastly, call function getEffectiveVotes() with both arguments wherever it is supposed to be called.
Cheers.
As you might have already about knew the error. This is due to trying to access the empty array or trying to access the value of empty key of array. In my project, I am dealing with this error with counting the array and displaying result.
You can do it like this:
if(count($votes) == '0'){
echo 'Sorry, no votes are available at the moment.';
}
else{
//do the stuff with votes
}
count($votes) counts the $votes array. If it is equal to zero (0), you can display your custom message or redirect to certain page else you can do stuff with $votes. In this way you can remove the Notice: Undefined offset: 0 in notice in PHP.
If you leave out the brackets then PHP will assign the keys by default.
Try this:
$votes = $row['votes_up'];
$votes = $row['votes_down'];
In my case it was a simple type
$_SESSION['role' == 'ge']
I was missing the correct closing bracket
$_SESSION['role'] == 'ge'
If you are using dompdf/dompdf and error occure in vendor/dompdf/dompdf/src/Cellmap.php then
It looks like we're using the wrong frame id in the update_row_group method. Initial testing seems to confirm this. Though that may be because this is strictly a paged table issue and not too many of the documents in my test bed have paged tables.
Can you try changing line 800 to:
$r_rows = $this->_frames[$g_key]["rows"];
($g_key instead of $r_key)
https://github.com/dompdf/dompdf/issues/1295
Use mysql row instead
mysql_fetch_row($r)
Meanwhile consider using mysqli or PDO
?>
Try seeding data with command: php artisan db:seed.
its just a warning use:
error_reporting(0);
it shows when we do not initialize array and direct assigning value to indexes.
somefunction{
$raja[0]="this";
$raja[1]="that";
}
instead :
somefunction{
$raja=array(0=>'this',1='that');
//or
$raja=array("this","that");
}
it just notification, do not generate any output error or any unexpected output.