webdeveloper123
Members-
Posts
437 -
Joined
-
Last visited
-
Days Won
1
Everything posted by webdeveloper123
-
deriving age from someone's birthday
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
foreach ($table as $value){ $date1=date_create("now"); $date2=date_create($birthday); $diff=date_diff($date1,$date2); } -
deriving age from someone's birthday
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
btw, I did a var_dump($birthday) and I only got this: string(10) "1988-08-19" Why is there only ever one value? -
deriving age from someone's birthday
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
As I said in my post, that's as close as I have got. I've been on it for 2-3 days now and wanted some help. -
deriving age from someone's birthday
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
I have tried it several different ways, as I said in my post I used SQL and 3-4 other pieces of code but it didn't work properly. I even tried the above code without a foreach and I got the same result. Yes It was bad design but i'm fixing it now. How can it be a single value(birthday) when It comes from the database of almost 200 records. -
Hello, I am trying to derive someone's age from their birthdate (which is stored in db) and print out age in years for every single record on a "View" page which lists all records from the database. Thing is it will only correctly calculate the age of the first record in the table and print out the same age for every single record. I've tried multiple ways including SQL statement using TIMESTAMPDIFF, but it only prints out the age of person for the first record. Here is some code. This is as close as I have got: <?php $query = "SELECT Form.FormId, Form.FirstName, Form.LastName, Form.Email, Form.Age, Form.Birthdate, Form.FavLanguage, GROUP_CONCAT(Vehicle.VehSelection SEPARATOR ', ') AS VehSelection FROM Vehicle RIGHT JOIN Form ON Form.FormId = Vehicle.FormId GROUP BY Form.FormId"; $result = mysqli_query($link, $query); if (!$result) { printf("Error in connection: %s\n", mysqli_error($link)); exit(); } $table = []; while ($row = mysqli_fetch_assoc($result)) { $table[] = $row; } if ( count($table) == 0) { //echo ("No records found"); exit; } else { $birthday = $table[0]["Birthdate"]; } foreach ($table as $value){ $date1=date_create("now"); $date2=date_create($birthday); $diff=date_diff($date1,$date2); } ?> <td><?php echo $diff->format("%y years"); ?></td> The SQL on the above code was meant for something else, but I thought seeing as I'm already pulling the birthdate from a SQL statement, on the same page, I thought i'd reuse it. Thanks
-
date not displaying on populated edit form
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
done it! thanks for the code Barand, very kind of you! -
date not displaying on populated edit form
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
thanks -
date not displaying on populated edit form
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
Hey barand are you sure the above is correct, because that's what I have down for DD-MM-YYYY? -
date not displaying on populated edit form
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
So straight changing the data type is not a good idea? -
date not displaying on populated edit form
webdeveloper123 replied to webdeveloper123's topic in PHP Coding Help
Yes I remembered but If I change the data type now, will it be ok or will I loose something in my db? -
Hi, I have a "View" records page where data is displayed from a database, and next to each record there is a "Edit" and "Delete" link. Problem is displaying the date on the "Edit" page. On the View page, it shows the dates correctly as they appear in the database. Now the funny thing is, if the date is in YYYY-MM-DD, it will populate on the Edit page, inside the field, correctly. But if the date is in DD-MM-YYYY format, it won't show on the edit page. The date is stored as varchar in the database, and about halfway through the database I changed the format for storage from YYYY-MM-DD to DD-MM-YYYY. This part seems to be ok. I thought the problem might be because originally it was YYYY-MM-DD then in php I changed it to DD-MM-YYYY so on the database end there is a mis-match of the date format and maybe that is what is the cause. And interestingly, if I click on a record which is YYYY-MM-DD, on the date field it will display in DD-MM-YYYY format, which is what I want. Here is some code: <?php $queryselect ="SELECT VehicleId, VehSelection, Form.FormId, Form.FirstName, Form.LastName, Form.Email, Form.Age, Form.Birthdate, Form.FavLanguage FROM Form LEFT JOIN Vehicle ON Form.FormId = Vehicle.FormId WHERE Form.FormId = '$FormId'"; $result = mysqli_query($link, $queryselect); $table = []; while ( $row = mysqli_fetch_assoc( $result ) ) { $table[] = $row; //add each row into the table array } if ( count($table) == 0) { //echo ("No records found"); exit; } else { $birthday = $table[0]["Birthdate"]; } $date=date_create($_POST['birthday']); $date1= date_format($date,"d-m-Y"); $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', email = '$email1', age = '$age1', Birthdate= '$date1', FavLanguage = '$fav_language1' WHERE FormId = '$FormId'"; $result1 = mysqli_query( $link, $updatequery ); ?> <label for="birthday">Birthday:</label><br> <input type="date" id="birthday" name="birthday" value="<?php echo($birthday); ?>"><span id="errorbday"></span><br> I have attached a screen shot of what the edit page looks like when I click on a record which is DD-MM-YYYY Thanks
-
Hey Barand, I decided to jump and try your solution, it works great. Not the layout I wanted and I wanted more on the "Edit" page by way of other data, but you weren't to know that. Thanks a lot, maybe I should have listened to you earlier. Im gonna try and change it to mysqli or add more data and HTML to your solution to make the layout/appearance the way I want. Thanks so much, and thanks for the patience!
-
There is a typo in the post where I copy/pasted advice from another forum, it should read: But that only ever generated the values in the db at the time, not all 3 all the time. Then I got this advice from another forum: @webdeveloper is me. someuser says :@webdeveloper You need to foreach() over the list of possible values, NOT the list of values that are already checked from the DB. someuser1 says: precisely; or, not use a foreach() loop at all, and just use separate if statements someuser1 says: foreach (['plane', 'yacht', 'car'] as $v) { $checked = isset($already_checked[$v]) ? ' checked' " ''; print "<input type=\"checkbox\" name=\"$v\" $checked />\n"; } Or something like that. webdeveloper says: ok thanks ill try that
-
Now I am getting conflicting advice. I was looping through the $table array and came up with this: <?php if (!empty($VehSelection)) { foreach($table as $key => $value) { $VehSelection= $value["VehSelection"]; ?> <input type="checkbox" id="<?php echo($value["VehSelection"]); ?>" name="vehicle[]" value="<?php echo($value["VehSelection"]); ?>" <?php echo ($VehSelection == 'Yacht' || $VehSelection == 'SuperCar' || $VehSelection == 'Plane' ) ? 'checked' : ''; ?> /> <label for="<?php echo($value["VehSelection"]); ?>" class="boxstyle"> <?php echo($value["VehSelection"]); ?></label><br> <?php }} ?> But that only ever generated the values in the db at the time, not all 3 all the time. Then I got this advice from another forum: @webdeveloper is me. @webdeveloper You need to foreach() over the list of possible values, NOT the list of values that are already checked from the DB. someuser someuser precisely; or, not use a foreach() loop at all, and just use separate if statements someuser1 foreach (['plane', 'yacht', 'car'] as $v) { $checked = isset($already_checked[$v]) ? ' checked' " ''; print "<input type=\"checkbox\" name=\"$v\" $checked />\n"; } Or something like that. webdeveloper webdeveloper ok thanks ill try that
-
This is my SQL SELECT VehicleId, VehSelection, Form.FormId, Form.FirstName, Form.LastName, Form.Email, Form.Age, Form.Birthdate, Form.FavLanguage FROM Form LEFT JOIN Vehicle ON Form.FormId = Vehicle.FormId WHERE Form.FormId = '$FormId'"; I understand about your database, I will do it on the next one. When I created this db, it was only the 2nd/3rd one in 15 years, so I didn't spot it straight away. I will take on board all comments and design the next one better. (as in the next project) Ok my db isn't that great and neither is my code, but it's not that bad surly that it won't work.
-
I have shown it, 6 posts up, dated Posted Monday at 02:37 PM (edited) I will put it again: $Vehicle[] = $table[0]["VehSelection"]; Here is when I fill the $table array with data from the result set of my Select statement (only showing relevant line) $VehSelection= $table[0]["VehSelection"]; And this is the print_r($Vehicle) for all 3 values (as in Plane,SuperCar and Yacht: Array ( [0] => Yacht ) Array ( [0] => Yacht ) Array ( [0] => Yacht ) Obviously that's not right, something going on there This is for 2 values, "Yacht" & "Plane" Array ( [0] => Yacht ) Array ( [0] => Yacht ) Array ( [0] => Yacht ) And this is for one value (SuperCar): Array ( [0] => SuperCar ) Array ( [0] => SuperCar ) Array ( [0] => SuperCar ) So i'm pretty sure it's the array that's going wrong. Having said that, for one value I get the desired result, even though the array is wrong. The $VehSelection variable that I have, if I loop around it and echo the variable I get what ever values that were for that record. So If there is a record which has one value (Yacht), that will print Yacht, correctly. If I click on a record that has 2 values (SuperCar, Plane) it will print SuperCar & Plane (correctly) And If I have all 3 (Yahct, SuperCar & Plane) when I click the record it will echo the 3 correct values. So, because in_array as the 2nd parameter must be an array, I though i'd take the $VehSelection variable and turn it into an arrray. That's why I did this: $Vehicle[] = $table[0]["VehSelection"]; But obviously I am not doing something right as you can see from print_r($Vehicle) results. Is there anyway I can use $VehSelection= $table[0]["VehSelection"]; as the 2nd parameter for in_array - I have tried many different variations but the error keep saying "2nd parameter must be array"
-
Hi, I've got this code now. It creates 3 check boxes all the time but only ticks 1, even if there are 2 or 3 values. Please can someone take a look: <?php $VehicleArray = array("Yacht", "SuperCar", "Plane"); if (!empty($VehSelection)) { foreach ($VehicleArray as $v) { $chk = in_array($v,$Vehicle) ? ' checked' : ''; echo "<input type=\"checkbox\" name=\"vehicle[]\" $chk />\n"; echo "<label for=\"$v\" class=\"boxstyle\"> I have a $v </label><br>"; }} ?>
-
This block of code now checks the box(but only 1 box, even if 2 or 3 were initially checked) and I am still only getting as many check boxes that were ticked, despite using in_array: foreach($table as $id=>$arr) { $chk = in_array($id,$Vehicle ?? []) ? 'checked' : ''; echo "<label class='boxstyle'><input type='checkbox' name='vehicle[]' value='$id'$chk>{$arr['VehSelection']} </label><br>\n"; }
-
Hi, I have added the following to my code: $Vehicle[] = $table[0]["VehSelection"]; And I also came up with: <?php foreach($table as $id=>$arr) { $chk = in_array($id,$Vehicle ?? []) ? ' checked' : ''; echo "<label class='boxstyle'><input type='checkbox' name='vehicle[]' value='$id'$chk> I have a </label><br>\n"; } ?> Forgetting about the label at the moment, it still only generates the number of check boxes that were originally ticked. I thought this code was supposed to always display 3 boxes, with the relevant once ticked. And it's not ticking the boxes either