-
Posts
3,404 -
Joined
-
Last visited
-
Days Won
55
Everything posted by Ch0cu3r
-
No, that wont work either. When using complex variable name like an associative array within a sting you need to wrap it within curly braces. You cannot escape the quotes for the variable The only time you'd escape quotes would be for the HTML, eg echo "<li><a href=\"profile.php?={$_SESSION['id']}\">My profile</a></li>"; // ^---- escape double quotes ----^
-
Wrap $_SESSION['id'] in curly braces echo "<li><a href='profile.php?={$_SESSION['id']}'>My profile</a></li>"; Or concatenate echo "<li><a href='profile.php?=".$_SESSION['id']."'>My profile</a></li>";
-
@Barand good catch Code amended // default order $order = 'projectId'; // get order from url? if(isset($_GET['order']) { // get order from url $order = $_GET['order']; // remember the column to order the results by. setcookie('order', $order, (time()+3600*24*365)); // Max lifspan 1 year } // is order set in cookie? elseif (isset($_COOKIE['order')) { // use order in cookie $order = $_COOKIE['order']; }
-
HTTP is stateless protocol nothing is remembered during HTTP requests. You'd use a cookie (or a session) to remember what table column to sort by. Something like // default order $order = 'projectId'; // is order set in cookie? if (isset($_COOKIE['order')) { // use order in cookie $order = $_COOKIE['order']; } // get order from url? elseif(isset($_GET['order']) { // get order from url $order = $_GET['order']; // remember the column to order the results by. setcookie('order', $order, (time()+3600*24*365)); // Max lifspan 1 year }
-
The variable $britbatID will only be defined if $_GET['britbatID'] is defined (ge your url is sitename.com/editbritbat.php?britbatID=1243). This is what this code does <?php if (isset($_GET['britbatID'])) { $britbatID = $_GET['britbatID']; }?> So if $_GET['britbatID'] does not exist then $britbatID will not be difined and so your get the undefined variable error. You are also redirecting your POST request to a GET request when the form is being submitted to itselft. This seems unnecessary. Don't perform the redirect, just replace $_GET with $_POST if (isset($_POST['britbatID'])) { mysql_select_db($database_britishforces, $britishforces); $britbatID = intval($_POST['britbatID']); $query_britbatname = "SELECT * FROM battalions WHERE battalions.britbatID = '$britbatID'"; $rsbritbatname = mysql_query($query_britbatname, $britishforces) or die(mysql_error()); $row_britbatname = mysql_fetch_assoc($rsbritbatname); // process result }
-
The sequence is simple. It is adding 1 to the previous number and then subtracting 7 if the new number is greater than 7, (resting the number sequence to 1 again) <?php // create emtpy array with 31 items set to zero $seq = array_fill(0, 31, 0); // set 1st, 15th and 26th postions with predifined number $seq[0] = 5; // 1st $seq[14] = 1; // 15th $seq[25] = 6; // 26th // generate sequance for($i = 1; $i < 31; $i++) { // if current postion in sequance is greater than 0, skip it // these are our predefined numbers in the sequance if($seq[$i] > 0) continue; // work out next number in sequance // gets the previous number and adds 1 $num = $seq[$i - 1] + 1; // if number is greater than 7, take 7 away if($num > 7) $num -= 7; // set the number $seq[$i] = $num; } ?> <style> th { color: red; width: 20px;} td { text-align: center; } </style> <table> <tr> <th><?php echo implode('</th><th>', range(1, 31)); ?></th> </tr> <tr> <td><?php echo implode('</td><td>', $seq); ?></td> </tr> </table>
-
If you are generating the form html in your PHP code then define the action as a variable. $action = "something or nothing"; $formhtl='<form action="'.$action.'" method="post">(form fields etc</form>';
-
You need to explain your problem in more detail. Such as how and when do you want the form action to change. Are you wanting to change the action when a user clicks a button/link? if so then this will be handled by javavscript not PHP.
-
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Ch0cu3r replied to jas21's topic in Applications
Then open/close the testimonial div within the while loop instead while($row = mysqli_fetch_array($results, MYSQL_BOTH)) { echo "<div class=\"testimonial\">"; # open testimonial echo $row['review'] ; echo "<div class=\"arrow-down\">"; echo "</div>"; echo "<div class=\"testimonial-author\">"; echo $row['name'] ; echo "</div>"; echo "</div>"."<br>"; # close testimonial } -
The x-axis is being plotted correctly. The problem you have is a 24 month gap in your data. The first data point is for 2011-05 the reset are for 2013-05. If you remove the first data point the graph is plotted correctly.
-
help updating a mysql database with a form
Ch0cu3r replied to waterwn1097's topic in PHP Coding Help
You do not have a field called state in your form. if ($_POST['state']) { Maybe change it to if(isset($_POST['Submit'])) { -
This is exactly what joins are for. Running a query on a result of another query is not very efficient. The following query does what you are trying to do SELECT n.id, n.name, n.man_no, # select these fields from names table s.score # select these fields from score table FROM names n # alias names table to the letter n LEFT JOIN scores s # join scores table and alias to letter s ON n.man_no = s.man_no # get the scores where the man_no matches WHERE s.score >= 0 # filter results where score is greater than zero # Result +----+------+--------+-------+ | id | name | man_no | score | +----+------+--------+-------+ | 1 | Alex | 12340 | 12 | +----+------+--------+-------+ | 2 | Anne | 12341 | 5 | +----+------+--------+-------+ | 3 | Ben | 12342 | 0 | +----+------+--------+-------+ But to answer your question all you need to do is check to see if the second query returned result $query = "SELECT * FROM names'"; $result = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($result)) { $name = $row["name"]; $man_no = $row["man_no"]; $query2 = "SELECT score FROM scores WHERE man_no='$man_no'"; $result2 = mysqli_query($dbc, $query2); while($row2 = mysqli_fetch_array($result)) { if(mysqli_num_rows($result2) > 0) { $score = $row2['score']; echo "$name - $man_no - $score<br />"; } } }
-
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Ch0cu3r replied to jas21's topic in Applications
Remove the foreach loop. You need to pass $result to mysqli_fetch_array while($row = mysqli_fetch_array($result)) { echo $row['review']; echo $row['name']; } -
Need to understand about ajax calls for phpfile
Ch0cu3r replied to gauravjain's topic in PHP Coding Help
A new database connection will be opened on each request. The first connection will be when you call abc.php and the second connection will be for the ajax request. PHP will close the database connection as soon as PHP has finished parsing the script. -
How do I enable another page to open if checkbox is checked
Ch0cu3r replied to Oscar11's topic in PHP Coding Help
this will never work <?php if($("input[@name=under19]:checked").val()=="private") { attr("action"clientdetails.php"); You cannot mix javascript (JQuery) code together with PHP code. They are completely different languages and both run at completely different times. PHP is ran on the server, but. Javascript runs in the browser long after PHP had parsed the script. You have two options when the checkbox is checked have JQuery submit the form to another page. Or Redirect the user to the other page when PHP processes the form. -
Change // group data into reigons (dR) $quakeInfo[ $json['dR'] ][] = $json; to // add quake data to array $quakeInfo[] = $json; Then use a single foreach loop to output your data foreach($quakeInfo as $quake) { // convert timestamp to english readable format echo date('F j, Y, g:i a', $quake['t']); }
- 15 replies
-
- preg_match
- preg_match_all
-
(and 1 more)
Tagged with:
-
There is nothing wrong with the code in the vote script. All three errors are caused by the first error which is [04-Jan-2014 11:22:22 UTC] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/pvpdream/public_html/voter/index.php:1) in /home/pvpdream/public_html/voter/index.php on line 2 I think the issue is with how the file encoding. Try changing your file encoding to UTF-8 without the BOM. You should have a file encoding option in your editor when you goto File >Save As...
-
Change // convert javascript json to be compatible with PHP $string = preg_replace('~:(new Date\([^)]+\))~', ':"$1"', str_replace("'", '"', $string)); to // convert javascript json to be compatible with PHP $string = str_replace("'", '"', $string); // convert the date into a timestamp $string = preg_replace_callback('~new Date\(([^)]+)\)~', function($dateParams) { // get the date parameters list($year, $month, $day, $hour, $min, $sec) = explode(',', $dateParams[1]); // remove the javascript math operation for the month $month = substr($month, 0, strpos($month, '-')); // create the timestamp return mktime($hour, $min, $sec, $month, $day, $year); }, $string); When you goto display the date later on you can convert the timestamp to english readable format (eg January 3, 2014, 11:37 pm) using date, eg foreach($quakeInfo as $region => $regionData) { echo "<h1>$region</h1>"; foreach($regionData as $quake) { // convert timestamp to english readable format echo date('F j, Y, g:i a', $quake['t']); } }
- 15 replies
-
- preg_match
- preg_match_all
-
(and 1 more)
Tagged with:
-
While you are still learning try to move your code over to mysqli The mysql_* functions are deprecated and could soon be removed.
-
Ohh, you're dealing with JSON. Code simplified futher preg_match_all('~VI.quakeInfo = \[([^\]]+)\]~', $data, $matches); preg_match_all('~\{[^}]+\}~', $matches[1][0], $data_array); $quakeInfo = array(); foreach ($data_array[0] as $string) { // convert javascript json to be compatible with PHP $string = preg_replace('~:(new Date\([^)]+\))~', ':"$1"', str_replace("'", '"', $string)); // decode the json $json = json_decode($string, true); // group data into reigons (dR) $quakeInfo[ $json['dR'] ][] = $json; } printf('<pre>%s</pre>', print_r($quakeInfo, true));
- 15 replies
-
- preg_match
- preg_match_all
-
(and 1 more)
Tagged with:
-
Accessing central php file, different domains, same server
Ch0cu3r replied to someone785's topic in PHP Coding Help
Not sure but you could add the common files into one folder on your server. Now add that folder to your include_path making the common files globally accessible. Alternatively setup a cron job and create a script which checks to see if one of the common files has changed. It'll then download and replace the updated file automatically. -
Because your are not defining these variables $FirstName, $LastName, $Email and $id You need to get these from $_POST first. $FirstName = mysql_real_escape_string($_POST['FirstName']); $LastName = mysql_real_escape_string($_POST['LastName']); $Email = mysql_real_escape_string($_POST['Email']); $id = intval($_POST['id']); $sql="UPDATE $tbl_name SET FirstName='$FirstName', LastName='$LastName', Email='$Email' WHERE id='$id'"; This is bad sql="INSERT INTO members (FirstName, LastName, Company, website, Category, Description, Address1, Address2, Email, City, State, Zip, OfficePh, CellPh) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[Company]', '$_POST[website]', '$_POST[Category]', '$_POST[Description]', '$_POST[Address1]', '$_POST[Address2]', '$_POST[Email]', '$_POST[City]', '$_POST[State]', '$_POST[Zip]', '$_POST[OfficePh]', '$_POST[CellPH]')"; Never insert raw post values directly into a query. You should always sanitize user input before using it within a query to protect yourself from SQL injection. PHP provides a function to help prevent this from happening called mysql_real_escape_string
-
The $expectedKeys array should contain the $_POST arrays keys (form field names) you are wanting to check. This is how you should $expectedKeys setup the array $expectedKeys = array('fname', 'lname', 'email', 'organization', 'supervisor', 'superemail', 'majcom'); The next problem you have is your database fields are named differently to your form fields. The code you are using to dynamically generating your SQL queries uses the keys listed in the $expectedKeys array as the actual database fields.
-
Yes you'll need to loop over the array. $data = array(); foreach($data_array[1] as $string) { // regex for finding keys $keyMatch = "#'(\w+)':#"; // match all keys preg_match_all($keyMatch, $string, $keys); // split string on keys $values = preg_split($keyMatch, $string); $values = array_filter($values); $values = array_map(function($value) { return trim($value, "',"); }, $values); printf('<pre>%s</pre>', print_r([$keys, $values], true)); // combine both arrays into key - value pairs $data[] = array_combine($keys[1], $values); unset($keys, $values); } printf('<pre>%s</pre>', print_r($data, true));
- 15 replies
-
- preg_match
- preg_match_all
-
(and 1 more)
Tagged with: