I'm trying to make PHP run a function and then load a different php page using a different function. I combined the two functions into an IF statement. However the condition of the IF statement runs fine but the block (which is the header() function) does not run at all.
<?php # DISPLAY SHOPPING CART ADDITIONS PAGE.
# Access session.
session_start() ;
# Get passed product id and assign it to a variable.
if ( isset( $_GET['item_id'] ) ) $id = $_GET['item_id'] ;
# Open database connection.
require ( '..\connect_db.php' ) ;
# Retrieve selective item data from 'shop' database table.
$q = "SELECT * FROM shop WHERE item_id = $id" ;
$r = mysqli_query( $dbc, $q ) ;
if ( mysqli_num_rows( $r ) == 1 )
{
$row = mysqli_fetch_array( $r, MYSQLI_ASSOC );
if ( isset( $_SESSION['cart'][$id] ) )
{
function alert($msg) {
echo "<script type='text/javascript'>alert('$msg');</script>";
return true;
}
# Add one more of this product.
$_SESSION['cart'][$id]['quantity']++;
$itemName = $row["item_name"];
alert("Another $itemName has been added to your cart");
}
}
# Close database connection.
mysqli_close($dbc);
# Display footer section.
include ( 'includes/footer.html' ) ;
header( "refresh:0;shop.php" );
?>
If I simply put:
$itemName = $row["item_name"];
alert("Another $itemName has been added to your cart");
added();
the added() function runs first, which is strange as I thought PHP runs sequentially. Obviously I'm doing something wrong, as I'm still studying PHP