Jump to content

Using goto?


Go to solution Solved by AbraCadaver,

Recommended Posts

Hi

 

I am trying to use goto in my script to jump around some code that causes the header to be displayed when there is on data to fill the table.

 

The code uses MySQL to query a database and present the data in a table.  Prceddining the main code is some code to check whether any lines of data are found and if none it shows a message on the screen.  It then still runs the code that produces the table and consquently only shows the table header.  I have tried using the test for data with a goto label at the end of the code, but I get a parse error: Parse error: syntax error, unexpected T_STRING in /homepages/43/d344817611/htdocs/Test/index.php on line 119

 

Here is the code I am running:

<?php
include("RelHol_connect.php");
doDB();

//verify the Event exists
$verify_Event_sql = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Events.Faith_ID = Religion.ID
	WHERE DAY(Events.Event_Date)= Day(Now()) And MONTH(Events.Event_Date)=Month(Now()) And YEAR(Events.Event_Date)=YEAR(Now())
	ORDER BY Events.Event_Date ASC";
$verify_Event_res =  mysqli_query($mysqli, $verify_Event_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($verify_Event_res) < 1) {
	//this Event does not exist
	$display_block = "<p><em>No Religious festivals or holidays today.</em></p>";

}
	
if (mysqli_num_rows($verify_Event_res) < 1) goto End	


//gather the Events
	$get_Event_sql  = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Events.Faith_ID, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Events.Faith_ID = Religion.ID
	WHERE DAY(Events.Event_Date)= Day(Now()) And MONTH(Events.Event_Date)=Month(Now()) And YEAR(Events.Event_Date)=YEAR(Now())
	ORDER BY Events.Event_Date ASC";

	$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));
//create the display string
	$display_block .= "
<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
	<tr>
	<th>RELIGION</th>
	<th>EVENT</th>
	</tr>";	

	while ($Event_today = mysqli_fetch_array($get_Event_res)) {
		$Rel_Date = $Event_today['Event_Date'];
		$Rel_Event = $Event_today['Religion'];
		$Event_text = $Event_today['Event_Description'];

//add to display
$display_block .= "
<tr>
<td width=\"8%\" valign=\"top\">".$Rel_Event."<br/></td>
<td width=\"25%\" valign=\"top\">" .$Event_text."<br/></td>";	
		
	}	

//free results
	mysqli_free_result($get_Event_res);

//close connection to MySQL
	mysqli_close($mysqli);
	
//close up the table
$display_block .="</table>";
echo $display_block;
End:
?>

Can anyone help please!

Link to comment
https://forums.phpfreaks.com/topic/281194-using-goto/
Share on other sites

  • Solution

If you're using goto you're doing it wrong.  There are many other ways to do what you want.

 

1. Change your IF to do that stuff if there ARE rows

2. Instead of goto End you could just exit;

Edited by AbraCadaver
Link to comment
https://forums.phpfreaks.com/topic/281194-using-goto/#findComment-1445166
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.