webdeveloper123 Posted April 22, 2022 Author Share Posted April 22, 2022 I guess the real question was how do I get the derived ages into my View page? Because I had the SQL and it was working, all the guys who posted here posted code that derived the age correctly but I couldn't get it into my View page which looks like this: <?php if ($table) { foreach ($table as $d_row) { ?> <tr> <td><?php echo($d_row["FormId"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["FirstName"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["LastName"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["Email"]); ?></td> <td width="10"> </td> <td><?php echo $diff->format("%y years"); ?></td> <td width="10"> </td> <td><?php echo($d_row["Birthdate"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["FavLanguage"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["VehSelection"]); ?></td> <td width="10"> </td> <td><?php echo("<a href='edit3.php?user_id=" . $d_row["FormId"] . "'>Edit</a>"); ?></td> <td width="10"> </td> <td> <?php echo("<a href='delete_feedback.php?user_id=" . $d_row["FormId"] . "'>Delete</a>"); ?></td> </tr> <?php } } ?> That was the problem from the start, as before I posted I had the correct sql but couldn't manage to get it into my View page, which displays all records from the database. I was doing this: <?php if ($table) { foreach ($table as $d_row) { ?> <tr> <td><?php echo($d_row["FormId"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["FirstName"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["LastName"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["Email"]); ?></td> <td width="10"> </td> <?php } } ?> <?php foreach ($resultage as $row) { ?> <td><?php echo " {$row['age2']} <br>"; ?></td> <?php } ?> <?php foreach ($table as $d_row) { ?> <td width="10"> </td> <td><?php echo($d_row["Birthdate"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["FavLanguage"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["VehSelection"]); ?></td> <td width="10"> </td> <td><?php echo("<a href='edit3.php?user_id=" . $d_row["FormId"] . "'>Edit</a>"); ?></td> <td width="10"> </td> <td> <?php echo("<a href='delete_feedback.php?user_id=" . $d_row["FormId"] . "'>Delete</a>"); ?></td> </tr> <?php } ?> But when I loaded the page it looked a total mess. So I'm using Barands code at the moment which is this: $agequery = "SELECT timestampdiff(year, Birthdate, curdate()) as age2 FROM Form"; $resultage = mysqli_query($link, $agequery); foreach ($resultage as $row) { echo " {$row['age2']} <br>"; } Now how do I get echo " {$row['age2']} <br>"; into the top block of code in this post, replacing <td><?php echo $diff->format("%y years"); ?></td> ? Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595596 Share on other sites More sharing options...
Barand Posted April 22, 2022 Share Posted April 22, 2022 10 minutes ago, webdeveloper123 said: Now how do I get echo " {$row['age2']} <br>"; into the top block of code in this post, Answer: By thinking about what you are doing instead of throwing code around hoping it'll stick. You are attampting to output {$row['age2']} but your variable in that loop is $d_row Replace <td><?php echo $diff->format("%y years"); ?></td> with <td><?php echo $d_row['age2'] ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595597 Share on other sites More sharing options...
webdeveloper123 Posted April 22, 2022 Author Share Posted April 22, 2022 I can't do that because age2 is not inside $table Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595598 Share on other sites More sharing options...
Barand Posted April 22, 2022 Share Posted April 22, 2022 Why not? I thought you told us you were using timestampdiff(year, Birthdate, curdate()) as age2 in your query. Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595599 Share on other sites More sharing options...
webdeveloper123 Posted April 22, 2022 Author Share Posted April 22, 2022 (edited) I have2 separate queries Edited April 22, 2022 by webdeveloper123 Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595600 Share on other sites More sharing options...
Barand Posted April 22, 2022 Share Posted April 22, 2022 I give up. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595601 Share on other sites More sharing options...
webdeveloper123 Posted April 22, 2022 Author Share Posted April 22, 2022 Come on barand, look i'll explain. This is what I have: $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); $agequery = "SELECT timestampdiff(year, Birthdate, curdate()) as age2 FROM Form"; $resultage = mysqli_query($link, $agequery); The first $query puts all the VehSelections on one line. Then I save that into $result. Then I do another query called $agequery and save it into $resultage. Then I fetch the $result and save it to the $table array. $table = []; while ($row = mysqli_fetch_assoc($result)) { $table[] = $row; } So then can I save both results ($result and $resultage) into the same $table array? Because I'm not sure if you can do that. I tried but it didn't work(maybe i'm doing something wrong. So instead I create a new table array, called $tableage: $tableage = []; while ($rowage = mysqli_fetch_assoc($resultage)) { $tableage[] = $rowage; } So moving on from there, If I have $table and $tableage, I got the derived ages in $tableage and the rest of the data in $table. So when I loop round to print the records from the database I use: <?php if ($table) { foreach ($table as $d_row) { ?> <tr> <td><?php echo($d_row["FormId"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["FirstName"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["LastName"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["Email"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["Age"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["Birthdate"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["FavLanguage"]); ?></td> <td width="10"> </td> <td><?php echo($d_row["VehSelection"]); ?></td> <td width="10"> </td> <td><?php echo("<a href='edit3.php?user_id=" . $d_row["FormId"] . "'>Edit</a>"); ?></td> <td width="10"> </td> <td> <?php echo("<a href='delete_feedback.php?user_id=" . $d_row["FormId"] . "'>Delete</a>"); ?></td> </tr> <?php } } ?> But because the foreach says $table, I can't use $tableage in there as well. So I start a new foreach: <?php if ($tableage) { foreach ($tableage as $d_row1) { ?> <td><strong>new Age</strong></td> <td width="10"> </td> <td><?php echo($d_row1["age2"]); ?></td> <?php }} ?> My question is how do I combine the printed out results into one? I would like to replace <td><?php echo($d_row["Age"]); ?></td> with <td><?php echo($d_row1["age2"]); ?></td> And have the printed results in one foreach, not two. Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595602 Share on other sites More sharing options...
Solution Barand Posted April 22, 2022 Solution Share Posted April 22, 2022 You should've changed your initial query, adding in the age calculation (simple as that, just like the examples I've shown you!) $query = "SELECT Form.FormId, Form.FirstName, Form.LastName, Form.Email, TIMESTAMPDIFF(YEAR, Form.Birthdate, CURDATE()) as age2, 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"; Now you have $d_row['age2'] Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595603 Share on other sites More sharing options...
Barand Posted April 22, 2022 Share Posted April 22, 2022 BTW Your HTML markup "<td width='10'>" was obsolete over a decade ago. You should be using CSS. (Did you get that from the same book?) Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595604 Share on other sites More sharing options...
webdeveloper123 Posted April 22, 2022 Author Share Posted April 22, 2022 @Barand...….genius! I should have tried that......popped into my head a few days ago but for some reason I never tried to combine them! Yes it was from the same book....believe it or not ( I know it doesn't show on these forums) but I got a 2.1 in Software engineering at Uni. Just didn't code for 14 years after that. Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595605 Share on other sites More sharing options...
Barand Posted April 22, 2022 Share Posted April 22, 2022 1 hour ago, webdeveloper123 said: genius! Hardly, just common sense. But, as the old saying goes, "There's nought so rare as common sense". Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595606 Share on other sites More sharing options...
Barand Posted April 22, 2022 Share Posted April 22, 2022 5 hours ago, webdeveloper123 said: ( I know it doesn't show on these forums) but I got a 2.1 in Software engineering at Uni. There was no such thing as computing or software courses when I left school for uni. Back in those days IBM were predicting that, one day, every country would own a computer. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595608 Share on other sites More sharing options...
gizmola Posted April 23, 2022 Share Posted April 23, 2022 10 hours ago, webdeveloper123 said: That's why I'm looking into doing Zend courses, as it is a much better level of teaching This site is a tremendous resource: https://phptherightway.com/ In working with MySQL this resource is what I'd recommend, whether that be PDO or MySQLi. If you took a poll of all the developers who regularly visit this community, you will find that we are nearly 100% in favor of PDO over MySQLi, but either one will work. https://phpdelusions.net/pdo I heartily endorse this Free Youtube channel: https://www.youtube.com/c/ProgramWithGio He has a playlist that serves as a free PHP tutorial classl: I have reviewed quite a number of his videos, primarily in order to recommend them to people. I have been a professional software engineer for decades, and have coded with PHP since it was version 3, so I don't take an endorsement like this lightly. If you are a visual learner, and prefer a video course, then I would warrant this is a better and more current course than the vast majority of courses you might find on any of the many e-learning platforms like Udemy. Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595611 Share on other sites More sharing options...
webdeveloper123 Posted April 23, 2022 Author Share Posted April 23, 2022 thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/314721-deriving-age-from-someones-birthday/page/2/#findComment-1595614 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.