-
Posts
5,510 -
Joined
-
Days Won
185
Everything posted by mac_gyver
-
search update delete record in mysql table using a form
mac_gyver replied to hance2105's topic in PHP Coding Help
this is a very common activity/task/assignment. there are 100's of thousands of examples posted on the Internet to find and examine to see how others have done this. where exactly are you stuck at in your attempt do do this? -
you can store your data on your server any way you want. it does not matter what format it is received/retrieved as. you store it so that it makes sense for how you are going to use it and trying to efficiently search for properties by it's name, rating, birthday, or value requires that each of those values be stored in separate columns (unless of course your assignment is to show that you can come up with the query necessary to solve this, in which case you should be attempting to do this yourself rather than asking on a help forum.)
-
php is a server side scripting language. you need - 1) a web server 2) php installed and functioning on that web server 3) you browse to the URL of the php script on a web server, not to the php file itself. for a localhost'ed web server, the URL would be http://localhost/install.php
-
array_search() cannot find the string '158' in values that are like - "157+0", "155+1" you must either use a more advanced search or get your data into a form that you can find the key values. since you are going have to wasted a bunch of time trying to get this to work, just trash this cart and get/write one that stores the cart in an array like i already suggested.
-
the code you posted for putting the dot in front of the domain parameter was shown for a session_set_cookie_params() statement. did you do the same for the set_cookie() statement? and, if this problem is because of url's having/not having the hostname in front of the domain name, you should be - A) consistent in your coding throughout your site B) have a redirect in your .htaccess file to send all requests to one hostname variation of your domain name.
-
your array_search() for just the key value will never find anything in exploded $cart_check . to find a key '158', you would need to use a wild card/preg_match type of search because the values at that point are '158+x' to manipulate this cart, take the incoming $cart_check = '123+0,145+1,134+2,'; string and convert it to an array ($cart[123] = 0; $cart[145] = 1; ...) like i suggested. then you can find any key/quantity in it.
-
your cart data definition is very problematic and is resulting in far too much code for any action on the cart. just use an array, where the array index is the id and the value is the quantity, especially since your code is making an array out of the cart every time you reference it. just eliminate all that conversion code and store the cart as an array in the first place. once you have the cart in the form of an array (your existing code or my suggested way of storing it), you need to use array functions to test and access the data. count() would tell you how may elements are in the array. empty() would tell you if the array is empty. your error implies that there are not two elements in the array (index values start at 0, not 1) and you are trying to access the second one.
-
How to call to method from other Class at the same time
mac_gyver replied to lilmer's topic in PHP Coding Help
if Class::method(0); returns an instance of some result class, which it should so that you can have more than one of them at any time, then using $result->methodFromAnotherClass(); should work. However, since you didn't post the error you got that would have given us actual information about why it didn't work, you are pretty much on you own with that problem. if your question is instead how would you convert $result->methodFromAnotherClass() to a static call, that all depends on what the code is doing. you may need to completely rewrite it to allow for multiple sets of stored properties to support multiple instances of it. in general, using static methods/properties should be avoided, not embraced. -
cannot help you with parts of the relevant code you didn't put in your post and spelling errors are not really programming problems anyway.
-
you need a leading dot . on the domain value to match all hostnames/subdomains.
-
another possible reason for 'random' operation of cookies/sessions is if the hostname (with or without the www.) or path in the URL changes, depending on how you arrive at a page and then navigate around. since you are setting the cookie path parameter to a /, that leaves the hostname changing as a possible cause. you may have multiple versions of your url (with/without the www.) in links, redirects, shortcuts, bookmarks, or in your browser history, so you might be starting with one variation of the hostname and it works until you switch to a different variation of the hostname.
-
Replaced php and get HTTP 500 internel server error
mac_gyver replied to Chrisj's topic in PHP Coding Help
the most common reasons for a http 500 error from a .php script is a fatal parse error or a fatal runtime error and php's error_reporting/display_errors are not turned on. just the snippet of code you posted does not produce a fatal php error, though you may have introduced such an error depending on where and how you inserted those lines into the file and something that is dependent on the result from those lines might produce a fatal runtime error if data values are missing/wrong variables... you need to set php's error_reporting to E_ALL and either set display_errors to ON or set log_errors to ON in your php.ini (for a fatal parse error you cannot set them in your code because your code never runs) to find out what if any php detected errors there are. -
you can turn notices off, but you shouldn't, especially when learning php as you will also miss things like typo's and using wrong variable names. you will end up wasting hours of your time trying to find simple mistakes in your code if you hide any of the types of errors. the correct coding for a variable that might not exist is to test if it isset() before using the value. using the short form of an if(){}else{} statement the code would be - $var = isset($_GET["test"]) ? $_GET["test"] : '';
-
you would just use a foreach(){} loop. for your database query to get the calorie information, you should not run a query inside of a loop. since the foodTypes are the keys of the $_SESSION['food'] array, just get all the keys and run one query to get all the rows you need - $types = implode("','",array_keys($_SESSION['food'])); $query = "SELECT Food, Size, Calories FROM food where Food IN('$types')"; $result = mysqli_query($con,$query); $calories = array(); while($row = mysqli_fetch_assoc($result)){ $calories[$row['Food']] = $row; // store the rows using the Food as the key so that this information can be easily accessed when needed } as you loop over the $_SESSION['food'] array, the key (foodType) from this array can be used to get the corresponding calories/size information from the $calories array that was formed in the above code.
-
by using three arrays, you are making three times too much code. do this - and all your code will be simple - if (isset($_POST['add'])){ if (empty($_POST['foodType'])){ echo 'You need to select some food!'; } else { if(!isset($_SESSION['food'][$_POST['foodType']])){ $_SESSION['food'][$_POST['foodType']] = array('Quantity' => $_POST['fooDquantity'], 'Unit' => $_POST['quantityValue']); } } }
-
because you are using a class method, that also contains quotes as part of its syntax, inside of a php string, you need to enclose it in { } so that php can properly parse all of it - {$date->format('Y-m-d')} if you had php's error_reporting/display_errors turned on you would be getting a notice message at the use of $date->format('Y-m-d') because php thinks it is a reference to a property, not a reference to a method because php needs to be told that the ('Y-m-d') is part of the variable instead of being part of the string it is in. you should also form sql query statements in php variables so that you can echo them out to see exactly what they are for debugging purposes.
-
the $_FILES array is empty when you try to upload a file that is larger than the post_max_size setting. $_FILES['channel_pic'] won't be set and all your code is bypassed. you need to test for this condition first. next, even if you upload a file smaller than the post_max_size setting, the upload can fail with an error. you must test if $_FILES['channel_pic']['error'] is set and equal to (an == comparison) a zero to insure that the upload actually worked. you can also test if $_FILES['channel_pic']['error'] is exactly equal to (an === comparison) a zero to combine both tests into one.
-
php is a server-side language. all the php code in a file runs on the server when the page gets requested. only the resulting output (html, javascript, media) is send to the browser. javascript is a client-side language. all the javascript code runs in the browser after it has been received by the browser. to get a value from javascript (in the browser) to php (on the server), you must send that value as part of a http request from the browser to the server. you can either send it as $_POST or $_GET data and if you don't want the page to refresh, you would use AjAX to send the http request.
-
do you have php's error_reporting set to E_ALL and display_errors set to ON so that things like header errors on the setcookie() statement would be reported and displayed? you may be outputting something on the page(s) that is preventing the setcookie() statement from working, depending on how you are reaching the code. you should log the token values with a time stamp so that you can determine which ones are the correct ones/what order they are generated in.
-
find nearby location using zipcode lookup within certain radius
mac_gyver replied to rashgang's topic in MySQL Help
the portion of the query you posted only calculates the distance. how are you using that value in the query to determine which rows are returned? -
you do realize that if you don't understand the basics of what you are trying to do or the direction given in the replies that you should probably just hire someone to code this for you? the 'jargon' used in any activity isn't to exclude people, it's to shorten the amount of time it takes to communicate an idea, so that someone doesn't have to write a book each time to cover the basics, which is why there are books, tutorials, and classes to learn the basics from. initializing a (session) variable to a specific state if it doesn't exist - session_start(); // initialize to an empty array if it doesn't exist if(!isset($_SESSION['used'])){ $_SESSION['used'] = array(); } since you need to reference the SoalID at the time you echo the random question, you either need to explode the data string and get the first value or you can use the SoalID as the index for your $arr array when you retrieve the data from the query. assuming you can get the id value for the question you are echoing, storing it to the $_SESSION['used'] array - $_SESSION['used'][] = $SoalID;
-
Find and delete duplicates under the right condition
mac_gyver replied to PatrickPetersen's topic in PHP Coding Help
here's some coding hints for doing this using php code - 1) use fgetcsv() for reading/parsing each line from the csv file. 2) since you need a count of each street address, you would need to read through all the data once, storing the street addresses into an array, then use array_count_values() to get the count of each street address. 3) read through the data a second time to process it. for each street address, the array obtained in the above step tells you how many times that address exists in the data. you can take that count and the status value to produce the logic needed to ignore/remove any rows.- 19 replies
-
- csv
- dublicates
-
(and 3 more)
Tagged with:
-
the undefined index error is because you need to -
-
Google recaptcha problem trying to integrate into my own system
mac_gyver replied to justin7410's topic in PHP Coding Help
try it with the <table></table> tags inside the <form></form> tags. -
since you have provided the bigger picture of how you are using the information (outputting one question at a time), the method to prevent duplicates must be different. you must 'remember' which questions have been output and remove them from the 'pool' of questions. which is what i suggested in post #3 in this thread. i would store the SoalID id values in an array in a session variable, then don't select those ids in the query. if $_SESSION['used'] is an array of the id's of the questions that have been output (initialize to an empty array if it doesn't exist), adding the following to the query would only return questions that have not been used - $ids = implode(',',$_SESSION['used']); $not_in = empty($ids) ? '' : " AND SoalID NOT IN($ids)"; $sqlText="SELECT * FROM soal WHERE Difficulty='$levelStr' AND Topic='$topic' $not_in"; the previous code i posted would also need to simply output one (the first) element from the shuffled array (i.e. no foreach() loop.)