Formatting Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Formatting Data

Post by gregwhitworth »

Okay I am able to retrieve my SQL data, now I just need the information to be displayed in a collective chunk and then display it automatically:

Code: Select all

Like this:

News Heading 1 - Date1
Description-1
---------------------------------
News Heading 2 - Date 2
Description 2

etc....
The only thing I can accomplish at the moment is either:

Code: Select all

News Heading 1 - Date 1
But I have to manually enter $value into the news.php, every time someone updates the news database I don't want them to have to call me and tell me to add another $value to the page.

Or I can get this:

Code: Select all

News Heading 1
News Heading 2
News Heading 3
Date1
Date2
Date3
Description 1
Description 2
Description 3
etc.....
I want to tell it how to format the information once, and then the php repeat that process "x" amount of times.

Here is the php that controls the data:

Code: Select all

<?php

	$username = "root";
	$password = "";
	$hostname = "localhost";
	$select = "SELECT header,date,description FROM news"; 
	$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
	print ("Connected to MySQL<br>");
	
	$selected = mysql_select_db("biglakebaptist",$dbh) or die("Could not select biglakebaptist");
	print ("Selected biglakebaptist<br>");
	
	$result = mysql_query($select) or die("Could not select news");
	print ("Selected news table<br>");

	while($col=mysql_fetch_assoc($result))
	{
		$header[] = $col['header'];
		$date[] = $col['date'];
	}
	
	foreach ($header as $value) {
	}
			
	mysql_close($dbh);
	
?>

Then on news.php I just call this script and place the following:

Code: Select all

Heading: <?php $value ?>
Please help!!

--
Greg
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

why not do it all in your while loop?

Code: Select all

while($col=mysql_fetch_assoc($result))
        {
                echo $col['header']." - ".$col['date']."\n".$col['description']."\n------------------------------\n";
        }
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

That is freaking AWESOME.

I should have guessed it would have been simpler than my method. Thanks so much.

--
Greg
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Glad to help - but don't get too comfortable with that kind of technique, you'll eventually want to develop better MVC habits.

I only mention this because I feel slightly guilty....
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

MVC?

Please by all means fill me in. I am having an awful time finding great documentation on the best way to accomplish things. My current problem:

I have the date being displayed as: 1985-02-03

I need it to be: 02-03-1985

I even searched this forum, and there are multiple topics about it, but none really explain (in newb terms) how to accomplish the transformation. I tried using SELECT DATE_FORMATE within MySQL but I couldn't get it to work, I may have select the actual table. Anyways, any suggestions.

--
Greg
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post by stakes »

Hi greg.

I'm a beginner here so i might be pointing you in the wrong direction but i just wrote some code for myself for date conversion:

viewtopic.php?t=74847

You could use it but you'd have to get rid of the "-" from the string first.

I.E

1985-02-03

to

19850203

and then just:

Code: Select all

$dateOuput 	= new DateConversion();
echo $dateOuput->getDay($yourDateString);
echo $dateOuput->getMonth($yourDateString);
echo $dateOuput->getYear($yourDateString);
or whatever order you want them in.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Check out the PHP date() function. I stumbled on to it one lonely night... but that's a story for another time.

Also, strtotime() would be useful.
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Wouldn't mind hearing the story, but I had already stumbled upon it. I just don't know how to use it with the sql data. I have tried many outlets and theories but can't seem to make it work. I am not at all asking you to do the work for me, just point me in the right direction.

Currently, I have tried the following:

Code: Select all

$col['date'] = Date('and then the format I desire');

Strtotime seems perfect for use as a 'Pull Date' for when the news headings will disappear, but beyond that I can't come up with anything. Please help!

--
Greg
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

strtotime() creates a timestamp from a String representation of the date.

date() can create a string representation of the date from a timestamp.

When you put the two together, magic happens:

Code: Select all

echo date('m-d-Y',strtotime('1985-02-03'));
or, in your case:

Code: Select all

echo date('m-d-Y',strtotime($col['date']));
Check out the manual page for the date output symbols: http://php.net/function.date
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Thank you so much, and I love the light way that you go about explaining things, explains your awards.

My biggest problem in being a newbie to this arena of PHP, isn't the finding of functions as php.net has a glorious plethora of them, but the syntax as you expressed so nicely in your most recent post.

Thank you so much, I am much better at graphics and flash, so if you aren't good at those - I would love to return the favor.

--
Greg
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I live to serve 8)

It's always easier to learn by example - I learned most of what I know from code examples in the manual notes. You'll get the hang of it soon enough, most functions follow a similar syntax.

Stick around, answer what you know. It's an addiction!
Post Reply