Just working on a small University project to develop an e-commerce website. I have been provided some code and I am altering this to my needs.
I currently have a problem with getting some arrays to work within an option and select tag. The difference being each of these arrays has an action associated with it eg, moving onto another page.
The problem seems to be this action is not being used within the option tags. As seen here http://mkiddr.com/phptests/shopping/
Here is my original code before editing:
<body>
<div id="wrapper">
<div id="header">
<p>Home | Browse:
<?php
$q="SELECT c_id, c_name FROM sc_cat";
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)){
echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
}
unset($q); //unset query variable
mysqli_free_result($result); //free result
?>
</div>
<div id="content"><!-- note this is an opening tag -->
Here is my code edited (doesn't work)
<body>
<div id="wrapper">
<div id="header">
<p>Home | Browse:
<select>
<?php
$q="SELECT CategoryID, CategoryName FROM ProductCategories";
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)){
echo "<option value='category.php?id=".$row[0]."'>".$row[1]."</a></option>";
//display categories
}
unset($q); //unset query variable
mysqli_free_result($result); //free result
?>
</select>
</div>
<div id="content"><!-- note this is an opening tag -->
Would really appreciate if someone could give us a hand with this!
The problem is that option tag has no "action" to execute. You must use javascript to redirect user to selected option. Change SELECT tag to
<select onchange="window.location = this.value;">
try swopping out your while loop for the following:
while ($row = $result->fetch_row()) {
echo "<option value='category.php?id=".$row[0]."'>".$row[1]."</a></option>";
//display categories
}
And see if this should work
As #Stormherz mentions also to have this load automatically on selection you need to place the onclick method to the option on the select tag;
Related
I am trying to learn php together with MYSQL. I have built the database on on the from end I have written the code below to bring in the website title from the database which works. However, I have two questions.
How would I get the result to display in <h1> tags?
Is this the cleanest way of pulling this value through?
Any help or advise greatly appreciated
<?php
include 'connection.php';
$sql = "SELECT VariableValue FROM VariableValues WHERE VariableID = '1'";
$result = $conn->query($sql);
while($header = $result->fetch_assoc()) {
echo $header["VariableValue"]. "<br>";
}
?>
To get a single row from db, you could use this:
$header_title = implode(mysqli_fetch_assoc(mysqli_query($conn, "SELECT VariableValue FROM VariableValues WHERE id = 1 LIMIT 1")));
Put a php variable between a html tag :
<tag><?php echo $var; ?></tag>
Eg :
<title><?php echo $header_title; ?></title>
You can echo all types of tags in PHP echo
for example:
echo '<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div id="success_alert" align="center" class="alert alert-success">
<strong>Log in to download this chapter.</strong>
</div>
</div>';
I have echoed different types of bootstrap tags in echo. Similarly you can also echo all types of tags you want.
To displays it in tags, use <h1><?php echo $var; ?></h1> for example.
I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections
Ok, so I'm making a somewhat simple search feature that connects to a MySQL server.
From my understanding the way to do this is to:
Make a search page on the site.
The search page will then accept a query and run it in the .php you provide
A result page will come out.
This is the basic stock code i will use:
<html>
<body>
<form action="searchtemplate.php" method="post">
Part ID: <input type="text" name="partID" />
<input type="submit" /></form>
</body>
</html>
I got this from the w3 school site.. it's simple and it works.
The php file i'm using is:
<?php
/**
Template Name: My Template
*/
get_header(); ?>
<div class="clearfix left-sidebar">
<!--BEGIN #primary-->
<div id="primary">
<?php the_post(); ?>
<?php $con=mysqli_connect("server","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
$part_query = $_POST['partID'];
$result = mysqli_query($con,"SELECT * FROM Parts WHERE part_id = $part_query");
echo "<table border='1'>
<tr>
<th>part_id</th>
then rest of columns
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['part_id'] . "</td>";
then echo rest of columns
echo "</tr>";
}
echo "</table>";
mysqli_close($con); ?>
<!--END #primary-->
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer();
?>
Ok, so I am using wordpress and I set this php up in the main theme directory and it can be accessed as a template. My main problem is that the search page appears fine but when I click submit it takes me to a blank page and not to the result page. I'd like to know if this is the correct way of doing what i am trying to accomplish or if there is a better way.
Your form is likely posting to the template, searchtemplate.php, and not a Wordpress page using the template to execute the code.
<form action="searchtemplate.php" method="post">
The "action" parameter in your form tag should point to the actual page on your site that is using your custom template.
I need to add a total to the categories of an online classifieds website. These totals will dynamically change just as the categories can so it needs to update each time the page loads. I believe i need to use a getTotal command but don't know how or where to input it. Thanks in advance for any help.
The code currently looks like this:
<div class="submenu">
<?php
foreach($catList as $row){
?>
<div class="submenutxt"><?php echo $row->category_name;?> </div>
<?php
}
?>
</div>
Total number of rows in $catList:
<?php
echo "$total rows: <br />";
foreach($catList as $row) { ?>
echo (count($row))." items in this category";
...
I have a list of names in a database that i want to display one by one
(also for bonus points, another column in the database is a Boolean value for if a task is completed or not. if this is true i want the css content box background to be green instead of red.)
so how can i select a name from row one, put it to a PHP variable, then select the value from the "Name" column in row 2 and put that to another PHP variable or the next item in the array?
thanks for any help!
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="mngPWinCSS.css"/>
</head>
<body>
<?php
$dsn ='mysql:host=****.******.com;dbname=o****_**n';
$username='********';
$password ='******';
mysql_connect('localhost',$username,$password);
$query=mysql_query("SELECT Name FROM CLOAS_Team LIMIT 0,1");
$bob="dkajfk";
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 60; URL=$url");
$com[1]="i";
$com[2]="i";
$com[3]="i";
$com[4]="i";
$com[5]="i";
$com[6]="i";
$name=mysql_fetch_array($query);
?>
<div id="content">
<img src="logjpg.JPG" alt="Smiley face" height="50" width="200">
<h3>CLOAS Tracker</h3>
</div>
<div id="Content">
<?php
?>
<div id="complete">
<h3names>
<?php
echo $name['Name'];
?>
</h3names>
</div>
<div id="incomplete">
<h3names>Name2</h3names>
</div>
</div>
</body>
</html>
First you need to change your SELECT query to select all of the rows that you wish to display, perhaps by taking off the LIMIT clause. Something like this;
$result=mysql_query("SELECT Name FROM CLOAS_Team");
(This will get you all of the names in your table.)
Next, you need to loop through the results you got from this query, like so;
$names = array();
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['Name'];
}
This will put them into the array $names for you, which you can then work with. Instead of putting them into the array, you might want to output them immediately, perhaps like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div>
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>
However, you have many more errors in your code. Such as;
You can't just invent html elements called <h3names>
I doubt that you want to set the id attribute to 'incomplete'. An id should be unique, I expect you should be putting this in as a class (class = "incomplete")
I don't think your line header("Refresh: 60; URL=$url"); will do anything as your headers have already been sent to the page. If you want this line to work, it needs to be right at the top, BEFORE any output has been sent to the browser.
And for the bonus point, include the 'Completed' field in your query (if that is what it is called) and use this to add a style to each <div> element that you display in your loop. So your query might become;
$result=mysql_query("SELECT Name, Completed FROM CLOAS_Team");
And your loop would now be like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div style = "background-color:<?php echo $row['Completed'] == true ? 'green' : ' red'; ?>">
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>