Jump to content

webdeveloper123

Members
  • Posts

    437
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by webdeveloper123

  1. foreach ($table as $value){ $date1=date_create("now"); $date2=date_create($birthday); $diff=date_diff($date1,$date2); }
  2. btw, I did a var_dump($birthday) and I only got this: string(10) "1988-08-19" Why is there only ever one value?
  3. 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.
  4. 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.
  5. 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
  6. done it! thanks for the code Barand, very kind of you!
  7. Hey barand are you sure the above is correct, because that's what I have down for DD-MM-YYYY?
  8. So straight changing the data type is not a good idea?
  9. Yes I remembered but If I change the data type now, will it be ok or will I loose something in my db?
  10. 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
  11. Hey I understood your code and I have incorporated it into my script, using mysqli and a different sql statement. This way I don't have to re-write my entire "Edit" page as it had a lot of other code too.
  12. Hey Barand, the code you posted on page 1 of this thread, thats my next step isn't it? You spotted it early. The code Posted April 9 (edited) with the DELETE and INSERT statement. Am I correct?
  13. 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!
  14. Yes that is the key thing here to make this code work. I tried using explode to convert the string into an array, but when I use: echo $Vehicle[0]; echo $Vehicle[1]; echo $Vehicle[2]; only position 0 prints out, the others i get offset errors
  15. 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
  16. 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
  17. Could it be that i'm saving at position 0, and they are all over writing each other, instead of incrementing the array with the other values? I got the below code from a PHP book I bought, so I thought it was sound
  18. 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.
  19. 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"
  20. 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>"; }} ?>
  21. 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"; }
  22. 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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.