I'm using XAMPP and PHP for a basic website.
I've already done most of it apart from the Content Management part.
I have a basic form where you upload a picture and add all the details necessary. I need help with keeping the format the same, because there are currently three houses on there with prices and details that are coded in HTML.
How do I echo the details out in the similar format?
Security is not an issue, because there isn't any sensitive information being used and it's not going to be used publicly. All I need to know is how to format the details and pictures in a similar way?
I have attached pictures so that you can better understand what I need help with as well. Here is the code:
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="content_management";
$tbl_name="houses";
$housepic ="housepic";
$houseprice ="houseprice";
$housetype ="housetype";
$houseloc = "houseloc";
$housedesc = "housedesc";
$conn = mysqli_connect($servername, $username, $password, $dbname );
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="INSERT INTO $tbl_name (picture, price, type, location, description) VALUES ('$housepic','$houseprice','$housetype','$houseloc','$housedesc')";
if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<?php
$host="localhost";
$username="root";
$password="";
$db_name="content_management";
$tbl_name="houses";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while ( $row = mysql_fetch_assoc($result) ) {
echo "<img src='" . $row['picture'] . "'>";
echo $row['price'];
echo $row['type'];
echo $row['location'];
echo $row['description'];
}
?>
<div class="housepost">
<img src="img/houses/house_01.jpg">
<h2>£350,000</h2>
<p>2 bedroom detached house for sale</p>
<p>Deanfield Avenue, Henley-on-Thames</p>
<p>Set in the heart of Henley-on-Thames and just a short walk from Henley train station is this rarely available and spacious three bedroom apartment. Offered to the market with no onward chain the property benefits from off road parking.</p>
</div>
<div class="housepost">
<img src="img/houses/house_02.jpg">
<h2>£475,000</h2>
<p>2 bedroom detached bungalow for sale</p>
<p>Fair Mile, Henley-on-Thames</p>
<p>Set in the heart of the town centre in a quiet backwater this delightful single storey detached home is rarely available and well presented.</p>
</div>
<div class="housepost">
<img src="img/houses/house_03.jpg">
<h2>£600,000</h2>
<p>3 bedroom cottage for sale</p>
<p>Remenham Row, Henley-on-Thames</p>
<p>The English Courtyard Association and The Beechcroft Trust - synonymous with the very best in retirement housing since the 1980s. An extremely attractive three-bedroom cottage with landscaped riverside gardens in this much sought after location.</p>
</div>
<?php echo '<div class="housepost">
<img>$row <img>
<h2></h2>
<p></p>
<p></p>
<p>$row</p>
</div>' ?>
</div>
I have no idea what I'm doing with PHP and I'm surprised I made it this far but I have a beginner understanding. You can see the three hardcoded HTML divs in the code, and the last div is my failed experiment attempt with PHP. If you look at the house picture, you can see the format I want the PHP script to display the information as. (Currently refreshing the page automatically creates a record with the form box titles which I need to fix as well)
Please ask questions if you need me to clarify, and if you could help me that'd be really appreciated. I'm sure the answer is super simple, it just took me a while to explain all of this.
This is the database thing I am using:
This is the format of the house in a div that I need PHP to automatically create each time a house is uploaded:
this is an easy and fast way to accomplish what your looking for.
i figure your problem isn't understanding PHP, DB and HTML basics, but putting it all together - so:
read the code comments CAREFULLY to understand all steps and relationships between all those 3 things.
after you get it, you can follow this link: php_file_upload to learn how to upload the picture (which is a bit more complicated, but is a good test for your learning).
this is the code (all in the same page!!!):
<!-- HTML form to add a new house -->
<form method="post">
<!-- this is only giving and existing url, notice you should change this
if you want to upload an actual pic -->
<input type="text" name="housepic">
<br>
<input type="text" name="houseprice">
<br>
<input type="text" name="housetype">
<br>
<input type="text" name="houseloc">
<br>
<input type="text" name="housedesc">
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
//init DB vars
$servername="localhost";
$username="root";
$password="";
$dbname="content_management";
$tbl_name="houses";
//conect to db
$conn = mysqli_connect($servername, $username, $password, $dbname );
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
/* if request method is: "post", a form was submitted.
get the submitted form params, and update DB */
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
/* notice you should add here error handling, case user didn't fill
all params */
$housepic = ( isset($_POST["housepic"]) ) ? $_POST["housepic"] : "";
$housepic = ( isset($_POST["houseprice"]) ) ? $_POST["houseprice"] : "";
$housepic = ( isset($_POST["housetype"]) ) ? $_POST["housetype"] : "";
$housepic = ( isset($_POST["houseloc"]) ) ? $_POST["houseloc"] : "";
$housepic = ( isset($_POST["housedesc"]) ) ? $_POST["housedesc"] : "";
// update DB
$sql="INSERT INTO $tbl_name (picture, price, type, location, description) VALUES ('$housepic','$houseprice','$housetype','$houseloc','$housedesc')";
if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
/* we'll always get to this part.
notice, if a form was already submitted, we've just updated our DB
accordingly, so the houses list is already updated */
while ( $row = mysql_fetch_assoc($result) ) {
/* good practice: grab your params before,
for a nicer and reusable code */
$picture = $row['picture'];
$price = $row['price'];
$type = $row['type'];
$location = $row['location'];
$description = $row['description'];
/********************************************************************
this is the part you are asking about.
notice that echoing the HTML tags content is done
inside the WHILE loop,
which of course will be always in the same format...
*********************************************************************/
echo "
<div class=\"housepost\">
<img src=\"{$picture}\">
<h2>{$price}</h2>
<p>{$type}</p>
<p>{$location}</p>
<p>{$description}</p>
</div>";
}
/* don't forget to close your DB connection */
mysqli_close($conn);
First of all I don't understand what you mean by similar format. But I guess you need to do something like this
<?php
//Remove these variables and place them in a file like config.php
$servername="localhost";
$username="root";
$password="";
$dbname="content_management";
$tbl_name="houses";
$housepic ="housepic";
$houseprice ="houseprice";
$housetype ="housetype";
$houseloc = "houseloc";
$housedesc = "housedesc";
//Make a function with all this block like function inserhome
$conn = mysqli_connect($servername, $username, $password, $dbname );
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//Send a variable named $_REQUEST["doinsert"] from the referer page to check if to insert or not
if($_REQUEST["doinsert"] == true){
$conn = mysqli_connect($servername, $username, $password, $dbname );
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="INSERT INTO $tbl_name (picture, price, type, location, description) VALUES ('$housepic','$houseprice','$housetype','$houseloc','$housedesc')";
if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
//Make a function like function selecthomes
mysql_connect($servername, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name";
$result=mysql_query($sql);
/*Change made to print all tables using mysql_fetch_assoc. I would
recommend using pdo instedof mysql_fetch_assoc functions since they are
deprecated. check http://php.net/manual/en/book.pdo.php
check the deprecation http://php.net/manual/en/function.mysql-fetch-assoc.php*/
while ( $row = mysql_fetch_assoc($result) ) { ?>
<div class="housepost">
<img src="<?php echo $row['picture'] ?>">
<h2><?php echo $row['price'] ?></h2>
<p><?php echo $row['type'] ?></p>
<p><?php echo $row['location'] ?></p>
<p><?php echo $row['description'] ?></p>
</div>
<?php } ?>
Secondly you also said:
Currently refreshing the page automatically creates a record with the form box titles which I need to fix as well
Well it is obvious that this script will always insert a new record because each time you do an insert at the beginning of the php file, so inevitably it will insert a new record.
One thing you need to check is if the record is already inserted.
I added some functionality to demonstrate how to check to insert the record or not. It's best you should pass a variable from the url like http://mypage/inserthouse.php?doinsert=true or you can make a form from the referrer page and pass the variable from a hidden field.
You can make two buttons. One will only display the house records and the other will insert and then display the records with the newly inserted. So the first button will link to http://mypage/inserthouse.php?doinsert=true and the seconds http://mypage/inserthouse.php?doinsert=false or http://mypage/inserthouse.php
I suggest you should create a function for each action and check wither you need to execute an insert or not. Make the question, When should I insert a new record?
Also you will not need the variable name $tbl_name. It doesn't matter in your code and it makes the script longer.
You also need to use an include file for your variables,
$servername="localhost";
$username="root";
$password="";
$dbname="content_management";
$tbl_name="houses";
$housepic ="housepic";
$houseprice ="houseprice";
$housetype ="housetype";
$houseloc = "houseloc";
$housedesc = "housedesc";
Your code looks like you need to read some object oriented programming for php. Give this a shot, http://www.codecademy.com/courses/web-beginner-en-bH5s3/0/1
I would also recommend using a framework instead of making it your self. Your skills are not there yet to make your own cms. Go with MVC and Object oriented patters.
Check the zend framework it is very nice and easy to use.
I hope I helped