Jump to content

Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?




Photo

Help integrating php/mysql with existing html page.


  • Please log in to reply
37 replies to this topic - - - - -

#1 Brodel

Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Location: London UK, we rock on top like a toupee

Posted 09 April 2012 - 15:08

<p>Hello, I know little about web design so I hope this makes sense.

I am trying to integrate a very basic php site into an existing html website to create a blog. I have one page where I can enter info which is then published onto a page called blogentries.php which works fine on it's own. I want the info to be published into the blogentries div on my website that says "I WANT BLOG ENTRIES HERE" close to the bottom.

I thought it would be a simple case of making the connection to the database in the header and put the rest of the php into the div, but obviously there is more to it than that.

I was wondering if someone could look at the code for the two pages and explain how it should be done?

Thank you

blogentries.php
<?php
mysql_connect("localhost:8889","root","root");
mysql_select_db("Alexweb");
echo "<html><head><link rel='stylesheet' href='style.css' type='text/css'></head><body>";
$query = "select * from content";
echo "<p>Query: ".$query;
$result = mysql_query($query);

$rows = mysql_num_rows($result);
for($i=0;$i<$rows;$i++) {
$row = mysql_fetch_array($result);
echo "<div class='".$row['class']."'>";
echo "<p> Date: ".$row['date']."</p>";
echo "<p><h3>".$row['title']."<⁄h3></p>";
echo "<p><img src='".$row['image']."'></p></div><p></p>";
echo "<p>".$row['text']."</p>";
 
}

echo "</body></html>";
?>


blogpage in html
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJWatson - Blog</title>
<meta name="keywords" content=" art design, digital media design, graphic design, logo design, ">
<link rel="shortcut icon" href="favicon.ico" />
<!-- Load CSS -->
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!-- Load Fonts -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Serif:regular,italic,bold,bolditalic" type="text/css" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold" type="text/css" />
</head>

<body>

<!--This is the START of the header-->
<div id="topcontrol" style="position: fixed; bottom: 5px; left: 960px; opacity: 1; cursor: pointer;" title="Go to Top"></div>
<div id="header-wrapper">
  <div id="header">
    <div id="logo"><a href="index.html"><img src="images/logo.png" width="100" height="80" alt="logo" /></a></div>
    <div id="header-text">
	  <h4>Blog</h4>
	  <h6><a href="index.html">Home</a> → Blog</h6>
    </div>
  </div>
</div>
<!--END of header-->


<!--This is the START of the menu-->
<div id="menu-wrapper">
  <div id="main-menu">
    <ul>
	  <li><a href="index.html">Home</a></li>
	  <li><a href="about.html">About</a></li>
	  <li><a href="portfolio.html">Portfolio</a></li>
	  <li><a class="selected" href="blog.html">Blog</a></li>
	  <li><a href="contact.html">Contact</a></li>
    </ul>
  </div>

<!--This is the START of the footer-->
<div id="footer">
  <div id="social-box">
   <ul>
    <li>
	 <div class="facebook"><a href=""></a></div>
    </li>
    <li>
	 <div class="twitter"><a href=""></a></div>
    </li>
    <li>
	 <div class="linkedin"><a href=""></a></div>
    </li>
   </ul>
  </div>
  <h6></h6>
</div>
<!--END of footer-->

</div>
<!--END of menu-->

<!--This is the START of the content-->
<div id="content">
 
  <!--This is the START of the blog-->
 
    <div id="blogentries">
	 
	 
	  I WANT BLOG ENTRIES HERE
	 
	 
  </div>
<!--END of blog-->

</div>
<!--END of content-->

</div>
</body>
</html>



#2 winlonghorn

winlonghorn

    Resident Elite

  • 1,037 posts
  • Location: Erie, PA

Posted 09 April 2012 - 15:22

You know....There was a time when people here would answer questions and help people out quite promptly. I don't know what the reasoning is, but it seems to me like that time has passed! I joined this site because the community was helpful honestly, but I certainly don't get the feeling that anybody wants to help anyone now! I apologize that I am unable to help Brodel. If I could remember how to do that, I would be glad to help out. I just wish someone else would at least acknowledge when people post asking for help. :( If anybody can clarify why that seems like it has changed, I would be grateful!

#3 OP Brodel

Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Location: London UK, we rock on top like a toupee

Posted 09 April 2012 - 15:27

My thread is only about 10 minutes old and people were very helpful the last time I made a post here. Thanks for taking the time to check it out too. :)

#4 winlonghorn

winlonghorn

    Resident Elite

  • 1,037 posts
  • Location: Erie, PA

Posted 09 April 2012 - 15:33

My thread is only about 10 minutes old and people were very helpful the last time I made a post here. Thanks for taking the time to check it out too. :)


Yeah, I figured it wasn't very old. I was just saying that I have run across many instances where the post just sat and sat and sat with no response (including my own posts). That is what concerns me. There should at least be a response saying that someone doesn't know or something like that so people aren't left hanging. :) You are very welcome. Sorry that I couldn't be of more assistance.

EDIT: I am not saying that everyone that sees it should post that they don't know, but at least somebody should take the time to come in and reply is all.

#5 winlonghorn

winlonghorn

    Resident Elite

  • 1,037 posts
  • Location: Erie, PA

Posted 10 April 2012 - 15:36

Well, I thought about it for a while and what if you were to do something like this:

<div id="blogentries">
		 
		 
		  <?php include 'blogEntries.php' ?>		 
		 
  </div>

and then give your page an extension of .php?

#6 threetonesun

threetonesun

    Neowinian ULTRAKILL

  • 11,398 posts

Posted 10 April 2012 - 15:41

Change this:
for($i=0;$i<$rows;$i++) {
$row = mysql_fetch_array($result);

to
while($row = mysql_fetch_array($result)){

//whatever you want to echo from the array here

}

See the PHP manual on mysql_fetch_array, it's very helpful. Basically, the while loop will run whenever $row evaluates to true, so as it iterates through the array that was returned, you can do whatever you want with each result. This is how most blog systems work. Also, you're going to have to include the blogentires.php code in the other file (either through include() or by pasting it in where you want it to appear).

It might help to download wordpress and take a look at how they do it. They use an object oriented approach, but it might help you understand where to make mysql connections, and where to call posts, etc.

Also, look into using mysqli, it can often be used in place of mysql, and is the newer/better implementation of it.

#7 OP Brodel

Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Location: London UK, we rock on top like a toupee

Posted 11 April 2012 - 14:11

Thanks guys, I managed to get it working with your help :) Just trying to figure the right way to set a maximum width for the images to be displayed at now. Seems trickier than I thought, as usual, lol.

#8 Simon-

Simon-

    Neowinian ULTRAKILL

  • 10,720 posts

Posted 11 April 2012 - 14:15

Yeah, I figured it wasn't very old. I was just saying that I have run across many instances where the post just sat and sat and sat with no response (including my own posts). That is what concerns me. There should at least be a response saying that someone doesn't know or something like that so people aren't left hanging. :) You are very welcome. Sorry that I couldn't be of more assistance.

EDIT: I am not saying that everyone that sees it should post that they don't know, but at least somebody should take the time to come in and reply is all.

The post was only 10 minutes old when you started posting this and what you are saying doesn't make sense because then someone would post "Nobody knows" after X amount of days (which in itself is a useless post) and then someone else who does know could then come later so how would anyone know that "Nobody knows" when they don't know who else could potentially see it.

#9 threetonesun

threetonesun

    Neowinian ULTRAKILL

  • 11,398 posts

Posted 11 April 2012 - 14:16

You can set max-width with CSS. You can also set them to a % and set a max and min on the column that contains them.

#10 winlonghorn

winlonghorn

    Resident Elite

  • 1,037 posts
  • Location: Erie, PA

Posted 11 April 2012 - 16:56

The post was only 10 minutes old when you started posting this and what you are saying doesn't make sense because then someone would post "Nobody knows" after X amount of days (which in itself is a useless post) and then someone else who does know could then come later so how would anyone know that "Nobody knows" when they don't know who else could potentially see it.


Alright, point taken. I guess it is just my frustration getting to me when it comes to trying to solve an urgent issue and not being able to find any help. I apologize.

Thanks guys, I managed to get it working with your help :) Just trying to figure the right way to set a maximum width for the images to be displayed at now. Seems trickier than I thought, as usual, lol.


No problem. I was glad to be able to help. :)

#11 winlonghorn

winlonghorn

    Resident Elite

  • 1,037 posts
  • Location: Erie, PA

Posted 11 April 2012 - 16:57

Mods please delete this post. I posted it thinking that the system would merge my posts like it has in the past. Sorry about that.

#12 OP Brodel

Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Location: London UK, we rock on top like a toupee

Posted 12 April 2012 - 01:13

You can set max-width with CSS. You can also set them to a % and set a max and min on the column that contains them.


Yea, I understand how it's done in CSS and tried that but couldn't get it to work. I'm still trying to get my head around integrating the html within the php properly. I'll take a look in the morning with fresh eyes.

#13 threetonesun

threetonesun

    Neowinian ULTRAKILL

  • 11,398 posts

Posted 12 April 2012 - 12:13

Yea, I understand how it's done in CSS and tried that but couldn't get it to work. I'm still trying to get my head around integrating the html within the php properly. I'll take a look in the morning with fresh eyes.


Just as a bit of help, I find it's much easier to look at php code with html mixed in when using alternative syntax. For example,

<?php
while($row = mysql_fetch_array($result)){
echo "<div class='".$row['class']."'>";
echo "<p> Date: ".$row['date']."</p>";
echo "<p><h3>".$row['title']."<⁄h3></p>";
echo "<p><img src='".$row['image']."'></p></div><p></p>";
echo "<p>".$row['text']."</p>"
}
?>

can also be written as:
<?php while($row = mysql_fetch_array($result)) : ?>
<div class="<?php echo $row['class']; ?>">
// whatever else you want to call
}
<?php endwhile; ?>
It's easier to figure out what's being generated by php, and what's in the html, and it becomes even easier to read if you ever switch over to object oriented php, as you can call things like:
<?php $post::title(); ?>


#14 OP Brodel

Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Location: London UK, we rock on top like a toupee

Posted 12 April 2012 - 18:59

Thanks, I can see why that is probably easier. I'll take a look at it next time I work with php. I've actually changed it a bit and added an option to upload a photo rather than to use a URL so now I'm just trying to figure out how to set it to resize it once it reaches a maximum width.

#15 OP Brodel

Brodel

    Neowinian UNSTOPPABLE

  • 5,877 posts
  • Location: London UK, we rock on top like a toupee

Posted 12 April 2012 - 21:45

I feel totally lost now. I've been looking online and there are a lot of really long scripts for resizing images. I'm not fussed about actually resizing images and saving them to another folder, just limit the width so it doesn't mess up my page. Modifying this a bit was the closest I got, but it resized and then displayed the entire directory. The thing is I can't do it for a specific file name because obviously it'll change with each upload. This is what I have so far.

php for adding title, date, text and uploading image.

<?php

//This is the directory where images will be saved
$target = "blogimages/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$title=$_POST['title'];
$date= date("d F Y H:i:s");
$photo=($_FILES['photo']['name']);
$text=$_POST['text'];

//This keeps the formatting of text entered into the text area
$text=nl2br($text);
// Connects to your Database
mysql_connect("localhost:8888","root","root") or die(mysql_error()) ;
mysql_select_db("Alexweb") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `blogentries` VALUES ('$title', '$date', '$photo', '$text')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The photo ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your entry has been added to the blog";
}
else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your photo.";
}

?>


php for pushing the posts to my blog

<?php

// Connects to your Database
mysql_connect("localhost:8888","root","root") or die(mysql_error()) ;
mysql_select_db("Alexweb") or die(mysql_error()) ;

echo "<html><head><link rel='stylesheet' href='style.css' type='text/css'></head><body>";

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM blogentries ORDER BY date DESC") or die(mysql_error());

//Puts it into an array
while($info = mysql_fetch_array( $data ))
{

//Outputs the image and other data
Echo "<h4></b> ".$info['title'] . "</h4>";
Echo "<b><h6></b> ".$info['date']."</h6><br>";
Echo "<p><img src=http://localhost:8888/uploadtest/blogimages/".$info['photo'] . "></p><br>";
Echo "<b><p></b> ".$info['text'] . "<p><br>";
Echo "<br>";

}

Echo "</body></html>";

?>