Jump to content

deriving age from someone's birthday


webdeveloper123
Go to solution Solved by Barand,

Recommended Posts

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>      ?

Link to comment
Share on other sites

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>
				

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

  • Solution

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']

Link to comment
Share on other sites

@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.

 

 

Link to comment
Share on other sites

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.

  • Haha 1
Link to comment
Share on other sites

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.   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.