Jump to content

PHP and HTML (Classes)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Hi guys!

I'm working on a website at the moment that uses both PHP and HTML is the files. Now iv created my classes ("Error","panel" and "ad"). What I'm looking for is a PHP script which I can implement to get data from a MySQL table and put it in a DIV in HTML.


For example:


<div class=error>

<div>

<?

IF $error == "1" 

{

This will be where I want the code to add a new div to the html and display the error. It needs to use the "error" class.

}

?>

</div>

</div>



Can somebody help me here or point me in the right direction?

Thanks!

#2
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Sorry guys, I completely wrecked the code section on this and 'Edit post' wont work.. so here's my edit.

I want it to show the "error" div if $error == "1"


thanks.

#3
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Well, I guess it would look something like this:

<?php
	
	class MyError
	{
		private $error_code;
		private $error_msg;
		
		public function __construct($x)
		{
			$this->setCode($x);
			$this->getMsg();
		}
		
		private function setCode($x)
		{
			$this->error_code = $x;
		}
		
		private function setMsg($x)
		{
			$this->error_msg = $x;
		}

		private function getMsg()
		{
			$q = mysql_query("SELECT * FROM errors WHERE id = '$error_code' ORDER BY id ASC LIMIT 1");
			$result = mysql_fetch_assoc($q);
			$this->setMsg($result);
		}
				
		public function displayMsg()
		{
			$array = $this->error_msg;
			
			echo '<div class="errorbox">
						' . $array['content'] . '
					</div>';
		}
	}
?>
			

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#4
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Thanks DEViANT,

I understand most of that, just all the "set code" bits. I need a simple approach though, nothing too detailed as I'm quite new to PHP. Infact, I started a day ago.

Say I call the div "error" and the message is "Could not log in".

I would put:

$error="1"
IF $error=="1" {
This is where I want it to show the text. 
}


Does that make it clearer?

Thanks.

#5
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
In that case, you could consider just using an array and echo'ing its content. Example :


<?php

	$error_code = 3;

	$error = array(

			'1' => '<div>This is an error</div>',

			'2' => '<div>This is another error</div>',

			'3' => '<div>This is some kinda error</div>',

			'4' => '<div>Why am I still typing?</div>',

			'5' => '<div>Sum Tin Wong</div>');

	echo $error[$error_code];

?>


otherwise it would be something like this :

<?php

	if($error_code == 1)

	{

		echo '<div>error</div>';

	}

	else if($error_code == 2)

	{

		echo '<div>Anotehr Error</div>';

	}

?>


Another Alternative :

<?php

	switch($error)

	{

		case 1 : echo '<div>error</div>';

					break;

		case 2 : echo '<div>Another Error</div>';

					break;

	}

?>


:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#6
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
I'd worked out another way of doing it, using the echo. But what you've just put back is perfect. Thankyou so much,

Kudos to you my friend!

#7
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
I think you are looking for this:

<?php

define('DOES_NOT_EXIST', 1);


$yourClass = new YourClass();

$yourClass->fetchRows(); // ...

?>

<?php if ($yourClass->getErrorCode() === DOES_NOT_EXIST): ?>

  <div class=error>

    <div>

      <p>Error occured: <?php $yourClass->getErrorMessage() ?></p>

    </div>

  </div>

<?php endif; ?>