I want to create elseif´s for each row...I can´t figure out how to do it...tried to google (elseif in while)...hope someone could give me a hint to solve this...
$subgalSql = mysql_query("SELECT * FROM galerieCategories ORDER BY title ASC");
while($subgalData = mysql_fetch_array($subgalSql)){
elseif($_GET[galeriepath] == $subgalData[title]){
?>HTML HERE<?
}
}
I hope someone can see what i´m trying to do here...the elseif are going to be subpages...
Thanks for any advice :)
"elseif" is only used as a secondary "if" statement.
if (condition) {
//if condition is true
} elseif (condition2){
//otherwise, if condition 2 is true
} else {
//all else
}
Long story short: what you want is just an "if", since it's the first (and only) condition.
This should work:
$subgalSql = mysql_query("SELECT * FROM galerieCategories ORDER BY title ASC");
while($subgalData = mysql_fetch_array($subgalSql)){
if($_GET[galeriepath] == $subgalData[title]){
?>HTML HERE<?
}
}
$subgalSql = mysql_query("SELECT * FROM galerieCategories ORDER BY title ASC");
while($subgalData = mysql_fetch_array($subgalSql)){
if($_GET[galeriepath] == $subgalData[title]){
//HTML HERE
}
elseif(/*some condition here*/){
// Code if it satifies the conditon
}
}
Ref: http://php.net/manual/en/control-structures.elseif.php
you cannot place an elseif inside the primary condition, it has to follow on after its end.
// what I think your trying to do
if()
{
while()
{
elseif()
{
}
}
}
what you should do
if($x='y')
{
$elsecondtion=false;
}
else
{
$elsecondtion=true;
}
$subgalSql = mysql_query("SELECT * FROM galerieCategories ORDER BY title ASC");
while($subgalData = mysql_fetch_array($subgalSql)){
if($elsecondition && $_GET[galeriepath] == $subgalData[title]){
?>HTML HERE<?
}
}
Related
How can I translate to PHP syntax something like that:
if(isset($_GET['q'])//but q=is not empty or <2 because in this case redirect to) { do this } else { do this one }
I hope it's not too massy.
Thanks.
updated question
Why this code does not redirect ?
if(isset($_GET['q']))
{
if(!empty($_GET['q']) || $_GET['q']>2)
{
$q = $_GET['q'];
$q = mysql_real_escape_string($q);
$sql = $db->prepare ('SELECT * FROM t WHERE a = :a');
$sql->bindParam(':a',$q);
$sql->execute();
}
else
{
header("Location:somepage.php");
}
} else {
$sql = $db->prepare ('SELECT * FROM t ORDER BY b');
$sql->execute();
}
Do like this
<?php
if(isset($_GET['q']))
{
if(!empty($_GET['q']) || $_GET['q']<2)
{
header("location:somepage.php");
}
else
{
echo "Cannot be redirected";
}
}
What? Do you mean the code for the "text" you have?
if(isset($_GET['q']) && (!empty($_GET['q']) || $_GET['q'] < 2)) {
// redirect here using header("location: foo.php") or some other function if you are using some framework.
}
else {
/otherwise do something else.
}
Update to your updated question:
Because $_GET['q'] isn't empty? Do a var_dump of $_GET so we, and you, know what's in there. It's impossible to tell otherwise. But I think your code is doing exactly what you have told it to do. You just don't have the complete picture of what you want to accomplish.
I assume q is a number here.
if (isset($_GET['q']) {
if (empty($_GET['q'] || $_GET['q'] < 2)) {
// do redirect here
} else {
// do mysql here
}
}
Why won't my search function ever execute the "else" (else should echo a text when no resulsts haved been found)? I also have some problems when trying to show all results (with no search criterias selected, just by pressing the search button). I'll upload the whole code of the page because I don't know if you need the HTML part as well or not to figure out the problem. I know it's a big chunk of code but please help out if you can. Thanks!
Here's a link to my code: http://pastebin.com/BXe1C0dr
This is not yet the answer, just a brief code structure of Matei Panchios code. Because it is hard to make sense of long code, so I try to simplify it so that other people might be able to help.
$termeni = mysql_real_escape_string($_POST['termeni']);
$tip=$_POST['tip'];
$judet=$_POST['judet'];
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
$query = "SELECT * FROM oferte WHERE localitate LIKE '%$termeni%' AND
tip_locatie='$tip' AND judet='$judet'";
// do the query and write some HTML
} elseif (isset($tip)) {
$query = "SELECT * FROM oferte WHERE tip_locatie='$tip'";
// do the query and write some HTML
} elseif (isset($judet)) {
$query = "SELECT * FROM oferte WHERE judet='$judet'";
// do the query and write some HTML
} elseif (!empty($termeni)) {
...
} elseif (!empty($termeni) AND (isset($judet))) {
...
} elseif (!empty($termeni) AND (isset($tip))) {
...
} elseif ((isset($judet)) AND (isset($tip))) {
...
} elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni))) {
...
} else {
// I believe this where it does not get executed.
}
Well, it makes sense why it does not get executed because there is other way that the elseif does not cover. If you look from this point of view
If three variable is set
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
If two variables is set
elseif (!empty($termeni) AND (isset($judet)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
If one variable is set
elseif (isset($tip))
elseif (isset($judet))
elseif (!empty($termeni))
When no variable is set
elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni)))
which leave else condition with nothing to cover.
If I were you, I would structure the code as following.
if (!empty($termeni) and isset($tip) and isset($judet)) {
query = '....';
} elseif (!empty($termeni) and isset($judet) {
query = '....';
} // .... the rest of the condition
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
// write HTML table
} else {
// write message that there is no result found
}
This will reduce the size of your code by 6 times.
hi i have several columns in my table if any of the column is empty it should be count as 1...at the same time if 2 or more columns empty in same row...it should not count as 2...
help me the mysql query.....
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$a=$row['ch1'];
$b=$row['ch2'];
$c=$row['ch3'];
$d=$row['ch4'];
$e=$row['ans'];
$f=$row['ques'];
}
}
else
{
echo "";
}
?>
if $a or $b or $c or $d or $e or $f is empty...it should count as 1...per row only once..not to count as 2 for the same row
Try this simple code
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
$empty_record = 0;
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$a=$row['ch1'];
$b=$row['ch2'];
$c=$row['ch3'];
$d=$row['ch4'];
$e=$row['ans'];
$f=$row['ques'];
if($a=='' || $b=='' || $c=='' || $d=='' || $e=='' || $f=='')
{
$empty_record++;
}
}
}
else
{
echo "";
}
echo $empty_record;
?>
I would try something like this:
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
$numOfEmpty = 0;
while($row=mysql_fetch_row($result))
{
for($i = 0;$i<count($result);$i++) {
if ($result[$i] == "") {
$numOfEmpty++;
break;
}
}
}
echo $numOfEmpty;
}
else
{
echo "";
}
?>
Please let me know however, if the code works. :)
I don't really understand your problem.
Maybe this will help - you can use the isempty() function, which will return true if your variable is empty.
Make a variable named empty and give it value 0. Then on your for loop add this:
if(isempty($a)||isempty($b)||isempty($c)||isempty($d)||isempty($e)||isempty($f))
$empty++;
I have a select box with an option for All, and then a list of users.
What I'm struggling with is creating something like this. I have most of it except trying to query the database to for it to check if the variable is in the database.
if ($variable == 'All') { code here }
else if ($variable != 'ALL' != *[result in database]*) { code here }
else { code here }
I have most of it except trying to query the database to for it to check if the variable is in the database.
Any suggestions how I can encorporate a query of a mySQL database in my if statement.
Thanks
if ($variable == 'All') {
... do something ...
} else {
$sql = "SELECT ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if ($row['somefield'] == 'whatever') {
... do something else ...
} else {
... do something even "elser" ...
}
}
You can't use operators like that.
Replace:
else if ($variable != 'ALL' != *[result in database]*) { code here }
With:
else if ($variable != 'ALL' AND $variable != *[result in database]*) { code here }
How do i validate $_GET thats the number coming from correct source.
My url look like : index.php?page=items&catID=5
When users put something like 3 which is doesn't exist on catID. I want it to display error message.
$catID = intval($_GET["catID"]);
if($catID) {
$checkSQL = mysql_query("SELECT * FROM category WHERE category_type='2'");
while($checkROW = mysql_fetch_array($checkSQL)) {
$checkCAT != $checkROW["categoryID"];
echo "err msg";
}
This i can come up so far but it doesn't working as it fire error msg even in correct page.
Thank you
wallk makes a good point, there is a missing if. but if i read this correctly, wouldn't something along the lines of this be more what you are going for? Right now the line:
if($catID) {
is actually only checking if catID (or, catID from the $_GET) is non-zero (not false). My guess if you are looking to check if catID is the categoryID returned from SQL?
$catID = intval($_GET["catID"]);
checkcat($catID);
function checkcat($check_category) {
$checkSQL = mysql_query("SELECT * FROM category WHERE category_type='2'");
while($checkROW = mysql_fetch_array($checkSQL)) {
if ( $check_category != $checkROW["categoryID"] ) {
echo "err msg";
} else {
echo "not an error message";
}
}
}
Expounding on what you are looking for, how about something like this then?
$catID = ($_GET["catID");
if ( !is_numeric($catID) ) {
echo "Not a numeric category!"
} else {
$checkSQLQuery = "SELECT * FROM category WHERE categoryID = '{$catID}' AND category_type='2'"
$resultSQL = mysql_query($checkSQLQuery, $db);
/* NOTE!: Guessing on what your database resouce
pointer is - it isn't included in the origin snippet.
Although, the last opened should be used by default if
this is left out. */
if ( mysql_num_rows($resultSQL) < 1 ) {
echo "Error message, category ID not found"
} else {
echo "Found it!"
}
}
Oh, I see. The first line inside the while loop should have an "if":
while ($checkROW = mysql_fetch_array($checkSQL)) {
if ($checkCAT != $checkROW["categoryID"])
echo "err msg";
It looks like you'll be wanting to use mysql_fetch_assoc(), rather than mysql_fetch_array().