Jump to content

Parse error: syntax error, unexpected ';'


phpsane

Recommended Posts

Hello Php People,

Can anyone spot why I am getting this error:

Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\test\trailers_stats.php on line 32

Line 32 looks like this:

 $query_type = "visitor_username"; 

	$visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 
    
    if ($visitor_type !== "") 
    { 
        $query_type = "visitor_type"; 
    } 
    elseif ($browsed_page_type !== "") 
    { 
        $query_type = "browsed_page_type"; 
    } 
    elseif ($browsed_page_url !== "") 
    { 
        $query_type = "browsed_page_url"; 
    } 
    elseif ($referral_page !== "") 
    { 
        $query_type = "referral_page"; 
    } 
    elseif ($searched_keywords !== "") 
    { 
        $query_type = "searched_keywords"; 
    } 
    else ($visitor_username !== "") 
    { 
        $query_type = "visitor_username"; 
    } 
	

Any ideas ?

Yes, I know. There is a php function to get the referral page but let's ignore that for the time being. Will deal with that later.

 

Thanks

Link to comment
Share on other sites

19 minutes ago, phpsane said:

I thought the last one is supposed to be just an "else".

http://php.net/manual/en/control-structures.elseif.php

Anyway, thanks. It solved the problem.

It's not a matter of whether it's the last one or not. It's matter of what you want it to do.

If you want to say "if this condition is true" then you want an else if. Just like the rest of the code was doing. If you want to say "if none of the other conditions were true" then you'd use an else.

Consider this:

// if $var is a number then none of these will execute and there will be no output
if ($var === true) {
	echo "true";
} else if ($var === false) {
	echo "false";
}

// in this version there will always be output
if ($var === true) {
	echo "true";
} else {
	echo "not true";
}

 

A safe coding practice is to make sure every conditional statement always has some branch that gets executed for any possible situation. That means if statements have an else and switch statements have a default.

Link to comment
Share on other sites

1 hour ago, requinix said:

It's not a matter of whether it's the last one or not. It's matter of what you want it to do.

If you want to say "if this condition is true" then you want an else if. Just like the rest of the code was doing. If you want to say "if none of the other conditions were true" then you'd use an else.

Consider this:


// if $var is a number then none of these will execute and there will be no output
if ($var === true) {
	echo "true";
} else if ($var === false) {
	echo "false";
}

// in this version there will always be output
if ($var === true) {
	echo "true";
} else {
	echo "not true";
}

 

A safe coding practice is to make sure every conditional statement always has some branch that gets executed for any possible situation. That means if statements have an else and switch statements have a default.

Thank you Requinix.

Your code sample jogged my memory a little.

Now you understand why I always ask for code samples. It saves time and either helps us learn something faster or helps us remind ourselves what we have forgotten. Not necessarily I would cut & paste your code in my script (like others always suspect in other forums and maybe suspect on this one too).

Therefore, next time I ask for a code sample then don't mind and tell mac Guyver the same.

One thing I learnt from your sample is that, the elseif could be the final one and the else does not always have to be the final one. See what your code sample just taught me and taught future newbies roaming this way ? :)

Thanks

Link to comment
Share on other sites


Php Folks,

Look at these 6 different types of urls. They have in common the "link_type=".

http://localhost/test/trailers_stats.php?link_type=visitor_type&visitor_type=unhappy visitor&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=page_type&page_type=info page&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=browsed_page_url&browsed_page_url=7search.com=25&page_number=
http://localhost/test/trailers_stats.php?link_type=referral_page&referral_page=7search.com&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=searched_keywords&searched_keywords=php forums&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=visitor_username&visitor_username=unique idea man&links_per_page=25&page_number=

And so, even though these are 6 urls, I do not want to be writing 6 different scripts but 1 with 6 different GETs:
    $visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 

Just incase you are wondering why I have these "visitor types" and "browsed page types" etc.
"Visitor Types" are: current visitor, previous visitor, lost visitors, happy visitors, unhappy visitors.
"Browsed Page" types are: information page, tangibles, intangibles.

So now you understand why I have these in my urls:
"link_types=visitor_type=lost_visitor",
"link_types=visitor_type=current_visitor",

"link_types=browsed_page_type=info_page",
"link_types=browsed_page_type=tangibles",

etc.

Now, I need 6 GETs to grab these from the url since I want to write only 1 script to deal with all these 6 urls and not 6 scripts. I mentioned the 6 GETs above.
I wrote the code like this:

    $visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 
    
    if ($visitor_type !== "") 
    { 
        $query_type = "visitor_type"; 
    } 
    elseif ($browsed_page_type !== "") 
    { 
        $query_type = "browsed_page_type"; 
    } 
    elseif ($browsed_page_url !== "") 
    { 
        $query_type = "browsed_page_url"; 
    } 
    elseif ($referral_page !== "") 
    { 
        $query_type = "referral_page"; 
    } 
    elseif ($searched_keywords !== "") 
    { 
        $query_type = "searched_keywords"; 
    } 
    elseif ($visitor_username !== "") 
    { 
        $query_type = "visitor_username"; 
    } 
    
    $query_1 = "SELECT COUNT(*) FROM searching_histories WHERE $query_type = ? 

PROBLEM NUMBER 1:
Problem is, on this url:
http://localhost/test/trailers_stats.php?link_type=visitor_type&visitor_type=unhappy visitor&links_per_page=25&page_number=

only the 1st GET gets triggered: $visitor_type = $_GET["visitor_type"]; 
And not the rest. That is why the other 5 GETs show error.

Notice: Undefined index: browsed_page_type in C:\xampp\htdocs\test\COMPLETE\trailers_stats.php on line ..

Notice: Undefined index: browsed_page_url in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: referral_page in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: searched_keywords in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: visitor_username in C:\xampp\htdocs\test\trailers_stats.php on line ..

So, for that url: http://localhost/test/trailers_stats.php?link_type=visitor_type&visitor_type=unhappy visitor&links_per_page=25&page_number=
I am getting errors on these lines:
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 

So, how to solve this problem ?
Here is the code:

    //Get the Page Number. Default is 1 (First Page). 
    $page_number = $_GET["page_number"]; 
    if ($page_number == "") 
    { 
        $page_number = 1; 
    } 
    $links_per_page = $_GET["links_per_page"]; 
    if ($links_per_page == "") 
    { 
        $links_per_page = 25; 
    } 
    
    $max_result = 100;     
    //$offset = ($page_number*$links_per_page)-$links_per_page; 
    $offset = ($page_number-1)*$links_per_page; 
    
    $visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 
    
    if ($visitor_type !== "") 
    { 
        $querying_column = "visitor_type"; 
    } 
    elseif ($browsed_page_type !== "") 
    { 
        $querying_column = "browsed_page_type"; 
    } 
    elseif ($browsed_page_url !== "") 
    { 
        $querying_column = "browsed_page_url"; 
    } 
    elseif ($referral_page !== "") 
    { 
        $querying_column = "referral_page"; 
    } 
    elseif ($searched_keywords !== "") 
    { 
        $querying_column = "searched_keywords"; 
    } 
    elseif ($visitor_username !== "") 
    { 
        $querying_column = "visitor_username"; 
    } 
    
    $query_1 = "SELECT COUNT(*) FROM searching_histories WHERE $query_type = ? ORDER BY id LIMIT ? OFFSET ?"; 

 

PROBLEM NUMBER 2:
I have mysql tbl columns under the variables: 
    $visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 

As you know, no matter which url you go to out of these 6:
http://localhost/test/trailers_stats.php?link_type=visitor_type&visitor_type=unhappy visitor&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=page_type&page_type=info page&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=browsed_page_url&browsed_page_url=7search.com=25&page_number=
http://localhost/test/trailers_stats.php?link_type=referral_page&referral_page=7search.com&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=searched_keywords&searched_keywords=php forums&links_per_page=25&page_number=
http://localhost/test/trailers_stats.php?link_type=visitor_username&visitor_username=unique idea man&links_per_page=25&page_number=

only 1 GET out of these 6 will get triggered:
    $visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 

Therefore, the mysql query would query 1 of these 6 columns: visitor_type, browsed_page_type, browsed_page_url, referral_page, searched_keywords, visitor_username.
And not query all of them.
So, this url:
http://localhost/test/trailers_stats.php?link_type=visitor_type&visitor_type=unhappy visitor&links_per_page=25&page_number=

would query the "visitor_type" column.
And this url:
http://localhost/test/trailers_stats.php?link_type=page_type&page_type=info page&links_per_page=25&page_number=

would query the "page_type" column. And so on.

Now, instead of writing 6 different mysql queries, I tried writing 1. 1 where the column name would be fed by the variable "$querying_column".
So my query looks like this:

$query_1 = "SELECT COUNT(*) FROM searching_histories WHERE $querying_column = ? ORDER BY id LIMIT ? OFFSET ?"; 

But I get error:
Fatal error: Uncaught mysqli_sql_exception: Unknown column 'querying_column' in 'where clause' in ...

So how to solve this issue ? Remember, I want to feed the column name via a variable as I don;t know which column would need to be queried. 
It all depends on the column referred to in the url (GET).

You are welcome to show me an example of how it should be done. Like Requinix showed earlier.

Link to comment
Share on other sites

21 minutes ago, requinix said:

Correct.

Incorrect.

How is this incorrect: The else does not always have to be the final one.

If the elseif can be the final one then that means the else does not always have to be the final one.

Look, the else is not the final one in your example:

// if $var is a number then none of these will execute and there will be no output
if ($var === true) {
	echo "true";
} else if ($var === false) {
	echo "false";
}
Link to comment
Share on other sites

Correction:

My error looks like this:

Notice: Undefined index: browsed_page_type in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: browsed_page_url in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: referral_page in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: searched_keywords in C:\xampp\htdocs\test\trailers_stats.php on line ..

Notice: Undefined index: visitor_username in C:\xampp\htdocs\test\trailers_stats.php on line ..

My code looks like this:

	    //Get the Page Number. Default is 1 (First Page). 
    $page_number = $_GET["page_number"]; 
    if ($page_number == "") 
    { 
        $page_number = 1; 
    } 
	    $links_per_page = $_GET["links_per_page"]; 
    if ($links_per_page == "") 
    { 
        $links_per_page = 25; 
    } 
    
    $max_result = 100;     
    //$offset = ($page_number*$links_per_page)-$links_per_page; 
    $offset = ($page_number-1)*$links_per_page; 
    
    $visitor_type = $_GET["visitor_type"]; 
    $browsed_page_type = $_GET["browsed_page_type"]; 
    $browsed_page_url = $_GET["browsed_page_url"]; 
    $referral_page = $_GET["referral_page"]; 
    $searched_keywords = $_GET["searched_keywords"]; 
    $visitor_username = $_GET["visitor_username"]; 
    
    if ($visitor_type !== "") 
    { 
        $querying_column = "visitor_type"; 
    } 
    elseif ($browsed_page_type !== "") 
    { 
        $querying_column = "browsed_page_type"; 
    } 
    elseif ($browsed_page_url !== "") 
    { 
        $querying_column = "browsed_page_url"; 
    } 
    elseif ($referral_page !== "") 
    { 
        $querying_column = "referral_page"; 
    } 
    elseif ($searched_keywords !== "") 
    { 
        $querying_column = "searched_keywords"; 
    } 
    elseif ($visitor_username !== "") 
    { 
        $querying_column = "visitor_username"; 
    } 
    
    $query_1 = "SELECT COUNT(*) FROM searching_histories WHERE $querying_column = ? ORDER BY id LIMIT ? OFFSET ?"; 
	
Link to comment
Share on other sites

16 minutes ago, requinix said:

You can't ignore the "if" next to it. It's an else if. Or elseif. Space or not it's the same thing.

I know the else if and elseif are the same.

I meant, the else does not have to be the final one. The elseif or the else if can be the final one TOO. Before your example, I thought the final one always has to be an else and it cannot be an elseif or an else if. Your sample code broke the myth in my thinking.

On your example, the elseif was the final one and not an else.

Your example was not this, where the else is the final one:

// if $var is a number then none of these will execute and there will be no output
if ($var === true) {
	echo "true";
} else ($var === false) {
	echo "false";
}

Your example was this, where the elseif was the final one:

// if $var is a number then none of these will execute and there will be no output
if ($var === true) {
	echo "true";
} else if ($var === false) {
	echo "false";
}
Link to comment
Share on other sites

5 minutes ago, phpsane said:

Your example was not this, where the else is the final one:


// if $var is a number then none of these will execute and there will be no output
if ($var === true) {
	echo "true";
} else ($var === false) {
	echo "false";
}

You know why it wasn't my example? BECAUSE IT'S NOT VALID.

If there is an else it must be the last one. If there is not an else then yeah, sure, else if can be the last one.

Link to comment
Share on other sites

Aw shoot, I didn't realize I had responded to uniqueideaman AKA uiman, forumcoder, sleep_in_code, phpsane, phpaid, site-developer, visiter52. I cant keep up with all the profiles and triplicate accounts he creates.

Dude, you have been at this well over two years now and you STILL dont understand how if/else works and STILL dont know what undefined indexes are about and how to handle them and STILL create variables for nothing after you have been told numerous times on several forums. Seriously, coding is not for you. You have wasted untold countless hours of experts time in numerous forums under your numerous alias and have been banned from forums more times than anyone in the history of forums. You STILL call people by pet names when you have been told NUMEROUS times not to do it by numerous members on numerous forums. Your just lucky I am not an admin of this forum. By now @requinix is well aware of your antics and he is a mod on several forums.

 

Link to comment
Share on other sites

1 minute ago, requinix said:

You know why it wasn't my example? BECAUSE IT'S NOT VALID.

If there is an else it must be the last one. If there is not an else then yeah, sure, else if can be the last one.

My point: elseif can be the last one in a valid condition.

Now, let us move-on to my 2 problems mentioned earlier.

 

Thanks!

Link to comment
Share on other sites

11 minutes ago, benanamen said:

Aw shoot, I didn't realize I had responded to uniqueideaman AKA uiman, forumcoder, sleep_in_code, phpsane, phpaid, site-developer, visiter52. I cant keep up with all the profiles and triplicate accounts he creates.

Dude, you have been at this well over two years now and you STILL dont understand how if/else works and STILL dont know what undefined indexes are about and how to handle them and STILL create variables for nothing after you have been told numerous times on several forums. Seriously, coding is not for you. You have wasted untold countless hours of experts time in numerous forums under your numerous alias and have been banned from forums more times than anyone in the history of forums. You STILL call people by pet names when you have been told NUMEROUS times not to do it by numerous members on numerous forums. Your just lucky I am not an admin of this forum. By now @requinix is well aware of your antics and he is a mod on several forums.

 

visiter52 ? phpaid ? Where did you get them ? They are not mine but the rest are.

You know, more than 9 mnths back, I was banned from here and few months ago you or someone got sleep_in_code banned in some forum. But 4 days ago, I find UI Man banned from devshed but guess what ? To my surprise I find phpsane working here and sleep_in_code working in that other forum. But you, as usual, got me banned again from that other forum. It won;t work here as Mac_Guyver and Requinix know PhpSane and UI Man are the same and so I ain't breaking their rules.

Keep it civil or both of us will get banned. requinix is not lenient here as she is in devshed. For many mnths, I could not figure why Requinix banned me from here (where I never boasted I would annihilate the major searchengines) but did not ban me over at devshed.

Say again, what ? You just realized I was UI Man ? I thought you already knew after Mac Guyver pointed-out in this thread that PhpSane and UI Man are the same. Just remember, I do not have double accounts here or at any forums. Using diferent Usernames at different forums is not against any forum's rules. Also, I am not cross posting this thread on other forums. If you have any doubts then google this thread's subject.

I am still puzzled to how Mac Guyver figured I was UI Man over at other forums. See if you can beat words out of him and then PM me his reply.

Link to comment
Share on other sites

Requinix,

Can you unban me from Devshed ? I miss Catacaustic over there. I guess the guys miss my boastings too. They never complained against my boastings. They were kept entertained. I asked you to ban me if I fail to finish my 1st project within 4 wks but I did finish it after the 1st wk. And so, I lived upto my dead-line. No need to ban me for that. If I suddenly go missing after 15 mnths of keeping the others waiting to see how my ventures fair then they all waited 15 mnths for nothing.

Anyway,. this thread is related to my 2nd project that I mentioned over at devshed. This project is nearly finished. It will compete with google, like I mentioned over at devshed.

Link to comment
Share on other sites

I find it difficult how one can have so much trouble understanding how an if statement works.

You first test a condition.  If it is true you have some code that you wish to execute.  Now if it is not true you have a decision to make about what to write next.  If the "not true" condition can be more than ONE thing, then you may want to write an elseif with a test of one more condition and, again, execute some code that handle THAT condition.  And again - if the alternative to this second test is more than one condition repeat the above.   When one finally gets to a place where this either there is only one condition left the else is sufficient.  If there is no other condition (meaning we have 'if'-ed ourselves thru them all) one doesn't need anything more.

This is not rocket science.

Link to comment
Share on other sites

19 minutes ago, benanamen said:

Dude, the only one that gets you banned is you!

 

LOL! You create so many profiles you cant even keep up with your self.

Believe me, them 2 Usernames (visitor52 & phpaid) are not mine. Visitor52 ? Maybe he was born in 1952 ? If so, his old enough to be my dad! Phpaid ? Do you reckon I am php expert enough to be giving others aid in php ? Use your common sense, man!

Which forums are they from ? I might aswell sign upto them!

Give me the 2 forums' names and I will see if I did sign upto them or not and what my Usernames there were. I never sign upto a forum with more than one account. Only did it at codingforums.com where I asked the mods to delete my 1st account before I opened the 2nd account. I never use more than 1 account at a forum simultaneously. If my Username becomes unpopular (like in codingforums.com) then I will ask the mods to delete my account and then I will signup with another. If you really must know, 2 of the mods knew I signed upto a 2nd account after requesting them to delete my 1st account. But they kept it hush giving me a chance to become a new person again so your mate Tango Force (and maybe you too and one or two others) get off my back with your naggings.

Frankly, I only did this once and over at codingforums.com. When I get banned at any forum, I don't bother signing up again under a new Username anymore. For some reason these forums track your mac address and ban your computer from signing up again under another Username.

I bookmark the forums I signed-up to and my Chrome remembers the Usernames & Passwords and auto fills them in. Believe me you, them 2 Usernames I do not use. Checking my Chrome Settings where usernames & Passwords get saved, them 2 Usernames are not found.

Anyway, tell me which 2 forums they are from. I want to check them out and investigate why you think those 2 are mine. Is there another person who boasts like me and dreams of annihalating the major searchengines and major social networks ? Maybe, I partner with him/her ? Lol! Really! Those 2 are not minee out of the 20 or so forums I joined. The first 10 forums I signed-upto used UiniqueIdeaMan. The rest used Usernames similar to the forum name but an opposite word. For example, this forum is called "phpfreaks". What is the opposite word for "freaks" ? "sane". Hence the Username "PhpSane". That is the signature I used for my Usernames in the last 10 forums out of the 20 I joined 12 months ago. 

Link to comment
Share on other sites

3 minutes ago, ginerjm said:

I find it difficult how one can have so much trouble understanding how an if statement works.

You first test a condition.  If it is true you have some code that you wish to execute.  Now if it is not true you have a decision to make about what to write next.  If the "not true" condition can be more than ONE thing, then you may want to write an elseif with a test of one more condition and, again, execute some code that handle THAT condition.  And again - if the alternative to this second test is more than one condition repeat the above.   When one finally gets to a place where this either there is only one condition left the else is sufficient.  If there is no other condition (meaning we have 'if'-ed ourselves thru them all) one doesn't need anything more.

This is not rocket science.

Don't worry. I understand this. I just forgot the logic and Requinix's code reminded me.

Link to comment
Share on other sites

 

15 hours ago, benanamen said:

Aw shoot, I didn't realize I had responded to uniqueideaman AKA uiman, forumcoder, sleep_in_code, phpsane, phpaid, site-developer, visiter52. I cant keep up with all the profiles and triplicate accounts he creates.

Dude, you have been at this well over two years now and you STILL dont understand how if/else works and STILL dont know what undefined indexes are about and how to handle them and STILL create variables for nothing after you have been told numerous times on several forums. Seriously, coding is not for you. You have wasted untold countless hours of experts time in numerous forums under your numerous alias and have been banned from forums more times than anyone in the history of forums. You STILL call people by pet names when you have been told NUMEROUS times not to do it by numerous members on numerous forums. Your just lucky I am not an admin of this forum. By now @requinix is well aware of your antics and he is a mod on several forums.

 

"You STILL call people by pet names when you have been told NUMEROUS times not to do it by numerous members on numerous forums." - Just what are you on about ? Who am I calling pet names on this forum and other forums apart from codingforums.com where I asked you if I could call you by "Banana Man" as that was easier to remember 2yrs back when I first came across you than remember "Benanamen" ? Do yourself a favour. Stop bickering. Stop digging old ghosts out of your closet. Flush them down the toilet and get on with your life. Understand ? I got better things to do like finish my current projects than argue with you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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