Echo out content from DB

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

05-Apr-12 08:59
Hello there smart fella!

I'm gonna explain this as good as I can whit my bad English, and my even worse php-skilllzzz

I've got, as usual, a header.php, where i store the code:

<?php
$query = "SELECT id, name FROM staticinfo";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{ echo "
<li><a href=.?action=viewInfo&amp;staticinfoId=$row[id]>$row[name]</a></li>";
}?>

This basically gives me the IDs of the table and echo out them in a <A> tag with the correct name.

In my index.php I have the code:

$results = array();
$data = Info::getList();
$results['staticinfo'] = $data['results'];
$results['pageTitle'] = "Info | ";
require( TEMPLATE_PATH . "/viewInfo.php" );


So, when I click the link in the header it leads to:
mypage/?action=viewInfo&staticinfoId=1
And here I want to display the info from the database with the Id=1, (or 2 depending on the <A tag>) and the content.

Right now in viewInfo.php, I got it to work with:

<?php foreach ( $results['staticinfo'] as $info ) { ?>
<h3><?php echo $info->name?></h3>
<p><?php echo $info->text?></p>
<?php } ?>


But I just want to display the name and text related to the Id=1, not the whole content from every Id.
The foreach{} gives me everything, as it gets all content from the DB.

But how do I manage to get just the name and text for Id 1?

Probably kind of basic, but I'm stuck and I need a smart and skilled person to help me aout

Best regards,
JJ
09-Apr-12 11:39
No one that can help? :/
09-Apr-12 14:42
It is done in the SQL NOT PHP

SELECT columnlist FROM table WHERE clause

If you put ONLY the column names you want and a WHERE clause that identifies the record/row you want, that's all you will get back.

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
09-Apr-12 17:06
Hi there chrishirst,

Sorry, but your explanation was too complex for my understanding. But my problem is solved, after a couple of hours

In my index.php Ive got (it works but I cannot understand it):

function viewInfo() {
if ( !isset($_GET["infoId"]) || !$_GET["infoId"] ) {
homepage();
return;
}
$results = array();
$results['info'] = Info::getById( (int)$_GET['infoId'] );
$results['pageTitle'] = $results['info']->name . " | KANSKE";
require( TEMPLATE_PATH . "/viewInfo.php" );}


And in the viewInfo.php-file, I replaced the code with:

<?php echo htmlspecialchars( $results['info']->name )?>
<p><?php echo ( $results['info']->text )?></p>


This will get the accurate content specified by the ID from the link, I assume, haha.

Well, it works

Thanks anyway
10-Apr-12 08:26
If that was too complicated you need to start learning basic SQL.


http://www.w3schools.com/sql/sql_select.asp

http://www.w3schools.com/sql/sql_where.asp

http://www.tizag.com/sqlTutorial/

Learning the basics of ANY language, coding, spoken or written is the key to understanding the rest of the language.

You say you don't understand how, why, or what you have done to "make it work", so it was basically luck then. What would you have done had you not been "lucky"?

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
10-Apr-12 08:45
Thank you for the links!

Yes, basically luck. I copied the function that editInfo uses, because this function does what I'm looking for. Then I copied all the html/php that uses the editInfo-function to my other file.
At last I cleaned up the code, and stripped it so I just had the parts that I needed. It worked, yay!

I'm certain there are other ways to do this, but I think I'm not using the worst Hehe.

Thank you again.

Peace!

 
New posts
Old posts

Follow Elated