I have a database with 4 columns, id, user, vipplan and expire.
What i want to do, is this:
i have my vipplan table, and:
if number 0 is in database: then display "N/A"
if number 1 is in database: then display "Sr.Pro"
if number 2 is in database: then display "No seas"
if number 3 is in database: then display "Level0"
But i do not know how to do it, i post my code:
<?php
echo "<h2>Resultados:</h2><p>";
if(isset($_POST['user']))
{
$find = $_POST['user'];
if ($find == "")
{
echo "<p>Olvidaste poner tu usuario -.-!!!";
exit;
}
mysql_connect("localhost", "root", "2513") or die(mysql_error());
mysql_select_db("invasionvip") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$iname = mysql_query("SELECT * FROM users WHERE usuario LIKE '%$find%'")
or die(mysql_error());
while($result = mysql_fetch_array( $iname ))
{
echo "id:" .$result['id'];
echo "<br>";
echo "usuario:".$result['usuario'];
echo "<br>";
echo "Plan VIP:".$result['planvip'];
echo "<br>";
echo "Vencimiento :".$result['vencimiento'];
echo "<br>";
}
$anymatches = mysql_num_rows($iname);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
echo "<b>Searched For:</b> " .$find;
}
?>
and my form is very simple:
<form class="pure-form" method="post" action="user.php">
<center>Aqui podrás ver tus servicios activos, como V.I.P, vehiculos, skins, etc.</center><br/>
<span style="text-align:left;font-size:25px;">Usuario: </span>
<input type="text" name="user">
<button type="submit" class="pure-button">Search</button>
</form>
thank you, :)
I believe what you are after is a case select. You should put this in the select line of your query.
there are multiple way that you can achieve this ourput
i thing the easiest way is switch statement second if else if and else third one is array notation which #b0s3 define earlier.
Switch example
switch(expression) {
case 1:
echo "Sr.Pro";
break;
case 2:
echo "No seas";
break;
case 3:
echo "Level0";
break;
default:
echo "N/A";
}
if else example
if( $databaseValue == 1 ){
echo "Sr.Pro";
}else if( $databaseValue == 2 ){
echo "No seas";
}else if( $databaseValue == 3 ){
echo "Level0";
}else{
echo "N/A";
}
Array Example
$yourValues = ['N/A', 'Sr.Pro', 'No seas', 'Level0'];
echo $yourValues[$databaseValue];
now it's up to your which one you like to use
You can use the "case" expression in the mysql select line.
This is a switch statement that allows you to return an arbitrary value for different conditions.
SELECT
CASE number
WHEN 0 THEN "N/A"
WHEN 1 THEN "Sr.Pro"
WHEN 2 THEN "No seas"
WHEN 3 THEN "Level0"
END CASE
FROM table_exemple
Related
I am building a pagination (SERP). Stuck on a single issue. The $_SESSION['row_count'] auto changes on every page load. Same page load. I can't figure-out why this unpermitted activity takes place by php.
This is a one page membership script (one page site) I am building. That means, register, login, logout, account homepage, search & pagination are not all different pages but one page where each have their own parts (functions).
The log & reg parts of the codes (functions) work. Only the keyword search & pagaination part of the script is failing due to the $_SESSION['row_count'] = 5 auto switching to $_SESSION['row_count'] = 0 on every page load.
Because my keyword search has 5 matches and due to me setting it to show 1 row per page, I should see 5 pages with a total of 5 rows or 1 row per page.
The PAGE 1 manages to show 1 matching row as expected. It's just, when I click any page (PAGE 2/PAGE3/PAGE4/PAGE5) then the $_SESSION['row_count'] = 5 auto switches to $_SESSION['row_count'] = 0 and so the sql_query fetches no rows.
//Grab total number of pages to paginate.
$row_count = $_SESSION['row_count'];//Pages beyong PAGE 1 this switches value from 5 to 0. That is BIG ISSUE! Why the switching ?
$total_pages = ceil($row_count/$result_per_page);
After clicking the SEARCH button, this part of my code yields $SESSION['row_count'] = 5. So far so good.
$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION['row_count'] = $row_count;
NOTE:
I am successfully shown the matching rows on PAGE 1. Since I set it to display 1 row per page, I am shown 1 matching row. So far, so good.
Now, when I click PAGE 2 on the PAGINATION section, I expect to see the 2nd matching row, but "$SESSION['row_count'] = 5" auto switches to "$SESSION['row_count'] = 0" and so no matching rows get shown. I repeat: Why the switching of values from '5' to '0' when I click PAGE 2 or onwards ?
This illegal switching ruins this following query that runs when I click PAGE 2 or any PAGE beyond PAGE 1:
$row_count = $_SESSION['row_count'];
//$total_pages = ceil($result_1/$result_per_page); //Should I keep this line or the line below ? Which one ?
$total_pages = ceil($row_count/$result_per_page); //Should I keep this line or the line above it ? Which one ?
CONTEXT:
<?php
error_reporting(E_ALL);
session_start();
?>
<!DOCTYPE HTML">
<html>
<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>
<?php
if(!isset($_GET['query_type']) && empty($_GET['query_type']))
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type']; echo __LINE__; echo "<br>";//DELETE
}
echo __LINE__; echo "<br>";//DELETE
if(!isset($_GET['form_type']) && empty($_GET['form_type']))
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type']; echo __LINE__; echo "<br>";//DELETE
if(!function_exists($_SESSION['form_type']))
{
die("Invalid Form!");
}
else
{echo __LINE__; echo "<br>";//DELETE
if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end')
{
$_SESSION['form_step'] = 'start'; echo $_SESSION['form_step'];
echo __LINE__; echo "<br>";//DELETE
$_SESSION['form_type']();
}
else
{
$_SESSION['form_step'] = $_GET['form_step'];
echo __LINE__; echo "<br>"; echo $_SESSION['form_step'];//DELETE
$_SESSION['form_type']();
}
}
}
//FUNCTIONS START FROM HERE
function search()
{echo __LINE__; echo "<br>";//DELETE
function rows_count()
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
if($conn === false)
{
die("ERROR: Connection Error!. " . mysqli_connect_error());
}
$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION['row_count'] = $row_count;
echo __LINE__; echo "<br>";//DELETE
$_SESSION['form_step'] = 'end'; //$form_step = 'end'; WRONG
//fetch_rows();
}
//Close Statement.
mysqli_stmt_close($stmt_1);
//Close Connection.
mysqli_close($conn);
}
function fetch_rows()
{ echo __LINE__; echo "<br>";//DELETE
$form_step = $_GET['form_step'];
$page_number = $_GET['page'];
$result_per_page = $_GET['page_limit'];
$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
$previous_page = $page_number-1;
$next_page = $page_number+1;
echo "Row Start: $offset";echo "<br>";
echo "Row End: $last_row_on_page";echo "<br>";
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
if($conn === false)
{
die("ERROR: Connection Error!. " . mysqli_connect_error());
}
$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))
{echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 111.
mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 114.
//Grab total number of pages to paginate.
$row_count = $_SESSION['row_count'];
//$total_pages = ceil($result_1/$result_per_page);
$total_pages = ceil($row_count/$result_per_page);
echo "TOTAL PAGES: $total_pages<br><br>";
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{echo __LINE__; echo "<br>";
//Retrieve Values.
$id = $row["id"];
$first_name = $row["first_name"];
$middle_name = $row["middle_name"];
$surname = $row["surname"];
$gender = $row["gender"];
$marital_status = $row["marital_status"];
$working_status = $row["working_status"];
echo "Id: $id<br>";
echo "First Name: $first_name<br>";
echo "Middle Name: $middle_name<br>";
echo "Surname: $surname<br>";
echo "Gender: $gender<br>";
echo "Marital Status: $marital_status<br>";
echo "Working Status: $working_status<br>";
echo "<br>";
echo "<br>";
}
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php
}
elseif($i==$page_number)
{
echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php
}
$i++;
}
if($page_number>$total_pages)
{
echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php
}
}
//Close Statement.
mysqli_stmt_close($stmt_2);
//Close Connection.
mysqli_close($conn);
$_SESSION['form_step'] = 'end';
//die();
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=1" method='post' enctype='plain/text'>
<?php
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "<label for=\"first_name\">First Name *:</label>
<input type=\"text\" name=\"first_name\" placeholder=\"First Name\" value = \"\">";?>
<br>
<?php
echo "<label for=\"marital_status\">Marital Status *:</label>";
echo "<select name=\"marital_status\">";
echo "<option value=\"single\">Single</option>";
echo "<option value=\"married\">Married</option>";
echo "</select>";
echo "<br>";
?>
<input type="submit" name="search" value="Search">
<?php
//$current_function = __FUNCTION__;
//echo $current_function;
//Do following if "Search" button clicked.
if($_SERVER['REQUEST_METHOD'] === 'POST')
{echo __LINE__; echo "<br>";//DELETE
//Do following if "Search" button clicked.
if(isset($_POST['search']))
{echo __LINE__; echo "<br>";//DELETE
rows_count(); //This function will forward script flow to fetch_rows() before halting the script.
fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? IT IS LINE: 200. MAIN ISSUE HERE, I SUSPECT.
echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 201.
die;
}
}
echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS FAILS TO ECHO. IT IS LINE: 198.
//Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 1, 2, 3, etc..
//rows_count(); //This function will forward script flow to fetch_rows() before halting the script.
fetch_rows(); //On PAGINATION PAGE 2, THIS FUNCTION IS NOT GETTING TRIGGERED! WHY ? IT IS LINE: 200. MAIN ISSUE HERE, I SUSPECT.
echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 201.
die;
}
?>
NOTE: I moved the session_start() to the top of the file underneath the error_reporting() but no luck. I even switched form method='post' to 'get' and tested. No luck. So, switched it back.
<?php
$conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
,"id6755695_user_info");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$department = $_POST["department"];
$year = $_POST["year"];
$sem = $_POST["semester"];
$reg = $_POST["regulation"];
$sql = "SELECT book_name, author, edition, image FROM dept_search WHERE
department='$department' AND year='$year' AND semester='$sem' AND
regulations='$reg' ";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if($row)
{
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
else
{
echo "sorry book not found";
}
}
mysqli_close($conn);
?>
please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....
As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.
What you can do is, check beforehand if the query returned actual results, like so:
$result=mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
}else{
echo "Sorry book not found";
}
You can try with mysqli_num_rows .. sample code as follows :
$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<br>";
?>
You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.
You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.
All you need to do is that you have to change if the condition from if($row) to if($other_condition)
Currently, you are just checking either there is something inside $row, and this condition will never be wrong unless you will assign it null. Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.
you have to simply one thing, that is to change if condition like given below...
if($row['value'] == 'something')
Hello i managed to make a simple search engine its basic so i started working on that..
But i get Access Forbidden and dont know why can someone help me?
with a form i give the $find the word i search and it should search for it..
thx for your time!`
<?php
if ($searching =="yes")
{
echo "Results";
if ($find == "")
{
echo "You forgot to enter a search term";
exit;
}
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error()); mysql_select_db("Ptyxiakh") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data =mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['document_name'];
echo " ";
echo $result['first_paragraph'];
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>"; }
echo "<b>Searched For:</b> " .$find; } ?>
ok i made it simpler to see if it works like this:
<?php
error_reporting(E_ALL);
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
mysql_select_db("Ptyxiakh") or die(mysql_error());
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE 'helmets'");
while($result = mysql_fetch_array( $data ))
{
echo $result['document_name'];
echo " ";
echo $result['first_paragraph'];
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
?>
But i get the "Sorry, but we can not find an entry to match your query<br><br>"; so i cant connect to database?its just wont work :(
With PDO it should be like this??:
<?php
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$pdo = new PDO('mysql:host=localhost;dbname=ptyxiakh', 'Anton', 'zouzou13');
// Otherwise we connect to our Database
//mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
//mysql_select_db("Ptyxiakh") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
//$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
$data= $pdo->query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
//And we display the results
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
echo htmlentities($row['document_name']);
echo " ";
echo htmlentities($row['first_paragraph']);
echo "<br>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
i made it work its pretty basic and i guess it has deprecated thingys but il upload for ppl that search for things...
just make a simple form and then make a php file
<?php
error_reporting(E_ALL);
//This is only displayed if they have submitted the form
echo "<h2>Results</h2><p>";
$find =$_POST["find"];
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
mysql_select_db("Ptyxiakh") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo '<p> <strong>',$result['document_name'], '</strong> <br> ', $result['first_paragraph'],'... <br> </p>';
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
?>
This worked for me :) thx!
I am having a big issue.
This is the first time I sue a foreach and I do not even know if it's the right thing to use.
I have a textarea where my members can add some text.
They also have all the accounts where to send the posted text.
Accounts are of two types F and T.
They are shown as checkboxes.
So when a member types "submit" the text should be INSERTED in a specific table for EACH of the selected accounts. I thought php foreach was the right thing. But I am not sure anymore.
Please take in mind I do not know anything about foreach and arrays. So please when helping me, consider to provide the modded code =D . Thank you so much!
<?php
require_once('dbconnection.php');
$MembID = (int)$_COOKIE['Loggedin'];
?>
<form action="" method="post">
<p align="center"><textarea id="countable1" name="addit" cols="48" rows="10" style="border-color: #ccc; border-style: solid;"></textarea>
<br>
<?
$DB = new DBConfig();
$DB -> config();
$DB -> conn();
$on="on";
$queryF ="SELECT * FROM `TableF` WHERE `Active`='$on' AND `memberID`='$MembID' ORDER BY ID ASC";
$result=mysql_query($queryF) or die("Errore select from TableF: ".mysql_error());
$count = mysql_num_rows($result);
if ($count > 0)
{
while($row = mysql_fetch_array($result)) {
?><div style="width:400px; height:100px;margin-bottom:50px;"><?
$rowid = $row['ID'];
echo $row['Name'] . '</br>';
$checkit = "checked";
if ($row['Main'] == "")
$checkit = "";
if ($row['Locale'] =="")
$row['Locale'] ="None" . '</br>';
echo $row['Locale'] . '</br>';
if ($row['Link'] =="")
$row['Link'] ="javaScript:void(0);";
?>
<!-- HERE WE HAVE THE "F" CHECKBOXES. $rowid SHOULD BE TAKEN AND INSERTED IN THE FOREACH BELOW FOR ANY SELECTED CHECKBOX IN THE FIELD "Type".SEE BELOW -->
<input type="checkbox" name="f[<?php echo $rowid?>]" <?php echo $checkit;?>>
</div>
<?
}//END WHILE MYSQL
}else{
echo "you do not have any Active account";
}
$DB = new DBConfig();
$DB -> config();
$DB -> conn();
$queryTW ="SELECT * FROM `TableT` WHERE `Active`='$on' AND `memberID`='$MembID' ORDER BY ID ASC";
$result=mysql_query($queryTW) or die("Errore select TableT: ".mysql_error());
$count = mysql_num_rows($result);
if ($count > 0)
{
while($row = mysql_fetch_array($result)) {
?><div style="width:400px; height:100px;margin-bottom:50px;"><?
$rowid = $row['ID'];
echo $row['Name'] . '</br>';
$checkit = "checked";
if ($row['Main'] == "")
$checkit = "";
if ($row['Locale'] =="")
$row['Locale'] ="None" . '</br>';
echo $row['Locale'] . '</br>';
if ($row['Link'] =="")
$row['Link'] ="javaScript:void(0);";
?>
<!-- HERE WE HAVE THE "T" CHECKBOXES. $rowid SHOULD BE TAKEN AND INSERTED IN THE FOREACH BELOW FOR ANY SELECTED CHECKBOX IN THE FIELD "Type".SEE BELOW -->
<input type="checkbox" name="t[<?php echo $rowid?>]" <?php echo $checkit;?> >
</div>
<?
}//END 2° WHILE MYSQL
}else{
echo "you do not have any Active account";
}
?>
<input type="submit" value="submit">
</form>
<?
//WHEN CHECKBOXES "F" ARE FOUND, FOR EACH CHECKBOX IT SHOULD INSERT INTO TableG THE VALUES BELOW, FOR EACH SELECTED "F" CHECKBOX
if(!empty($_POST['addit']) && !empty($_POST['f'])){
$thispostF = $_POST['f'];
$f="F";
foreach ($thispostF as $valF)
//THE MOST IMPORTANT FIELD HERE IS "Type". THE ARRAY INSERT "on", I NEED INSTEAD THE VALUE $rowid AS ABOVE
$queryaddF="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$f."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}
//WHEN CHECKBOXES "T" ARE FOUND, FOR EACH CHECKBOX IT SHOULD INSERT INTO TableG THE VALUES BELOW, FOR EACH SELECTED "T" CHECKBOX
if(!empty($_POST['addit']) && !empty($_POST['t'])){
$thispostT = $_POST['t'];
$t="T";
foreach ($thispostT as $valF)
//THE MOST IMPORTANT VALUE HERE IS "Type". THE ARRAY GIVES "on", I NEED INSTEAD THE VALUE $rowid AS ABOVE
$queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}
?>
foreach ($thispostT as $valF)
{
$queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}
please put start and ending bracket to your foreach loop and try i have not read the whole code but just found you missing the brackets. hope that helps you.
I think I know what you're doing...
You're going to need to do:
foreach($_POST as $key => $value) {
$type = substr($key,0,1);
$id = substr($key,1);
if($type == 't') {
// do insert for t table here
} else if($type == 'f') {
// do insert for f table here
}
}
I didn't test it but it should be something like this.
My suggestion is
create field name as t[] (array)
onchecked value will be passed on the next page
the form checkbox field should be like that
<input type="checkbox" name="t[]" value="< ?php echo $rowid?>" <?php echo $checkit;? > >
and when you Submit the form
GET THE VALUE and insert in to database;
< ?
if($_POST['t'])
{
foreach($_POST['t'] as $v)
{
queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}
}
? >
I need to echo all items in my column that have the value of three. If a field has the value of three I then need to echo the 'name' and 'description' on that row.
This is what I have so far
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}
I need to write 'if $row[`status´ == 3 echo 'description' 'name' else echo 'no current staus'
I hope I made some sense because I am seriously confused
You can actually select the result which only has 3 value in its status
for that use this
$query = "SELECT * FROM yourtable WHERE status='3'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
echo "Name: ".$row['name']."<br>Description: ".$row['description'];
}
and if you want to parse you result to display only those of value this then use this
$query = "SELECT * FROM yourtable";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
if($row['status']==3) {
echo "Name: ".$row['name']."<br>Description: ".$row['description'];
}
}
if $row['status'] == 3 {
echo ($row['description']." ".$row['name']);
}
else
{
echo ('no current staus');
}
The "." in the first echo means string concatenation. I'm separating description and name with a single space character, feel free to change this.
I'm expecting something more since you have the "i need to write" part that is the answer?
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) // fetch_assoc better for your case
{
if( $row['status'] == 3 ) {
echo $row['description'];
}else {
echo 'no current staus';
}
}
For the sake of info for tasks with a numeric status
switch( $row['status'] ) {
case 0:
// do something
break;
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
default:
echo 'No status set';
}
if ($row['status'] == 3) {
echo $row['name'];
echo $row['description'];
}
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if ($row['status'] == 3) {
echo $row['name'] . ' - ' . $row['description'];
}
else {
echo 'no curent status';
}
}
if($row['status']==3) {
echo $row['description'];
echo $row['name'];
} else {
echo 'no current status';
}
Maybe you should learn about basics of PHP and algorithms and make sure you understand this, before you move on.
This?
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if (!in_array(3, $row['status']))
{
echo 'no current staus';
break;
}
echo $row['description'] . <br />;
echo $row['name'] . <br />;
}
or you could use SQL to select the values for you...
$query1 = SELECT * FROM table WHERE column = '3';
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}