Jump to content

Display Database Info?


Drewdle

Recommended Posts

I'm currently trying...struggling....to teach myself PHP and I'm really bugged with this database stuff. I am slowly managing but I'm a tad bit stuck now.

 

I want to show a specific piece of information from a table.

 

Lets say my table is structured like so:

 

iduseremail

1

Bob

[email protected]

[email protected]

 

What would I need to do to display ONLY Freds user?

 

One way I tried only displayed the first rows info (Bob) the second way I tried (with a while loop) only displayed the last rows info (Matt)

 

Heres my current code:

<html>
<body>
<?php
include 'dbwire.php'; 

$query = mysql_query('SELECT * FROM user');

$row = mysql_fetch_array($query); 

while ($row = mysql_fetch_array($query)) { 
echo '<b>User:</b> ' . $row['user'] . '<br />';
}
?>
</body>
</html>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/223455-display-database-info/
Share on other sites

yes but what if fred isn't in $row[1]. You would need to check all of them. Btw I haven't coded in ages. Lol

 

Okay I looked at it again. And its like I said I haven't coded in awhile. I'm wrong it looks in them all lol.

Thanks for the help jake2891.

 

I've hit another snag.

 

Is it possible to select a field (like 'title') from a link on a page then display another field from the same row (like 'content') below the links?

 

Its like a page creator that you'd find on a cms but because I want to learn php rather than just copy it, I'd like to find some sort of sample I could play with.

 

What I have is a new table (content) with 'id', 'title' and 'content' fields, what I want is a user to click a link (title field entry) and then display the content associated with the title.

 

Example:

 

id      title          content

1  index.php  blahblahblah

2  about.php  blahblahblah

 

If they click a link named 'Main' it would collect content from id 1 and display it below.

 

If you need more explination, let me know.

 

Thanks on advance :)

Not the entire info of all three fields (id, title, content) Just the info from one field. (content).

 

I don't need to display the id or the title.

 

But other than that...pretty much yeah. :)

 

The same thing this chaps trying to accomplish: HERE

is this what u mean??

 

<html>
<head>
<script>

function show_content(id){
    document.getElementById(id).style.display='block';
}


</script>
</head>
</html>
<?php
// sudo code
foreach($results as $result){
    // results holding your records from your content table
    $title = $result['title'];
    $id = $result['id'];
    $content = $result['content'];
    echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>";
    echo "<div id=\"$id\" style=\"display:none\">$content</div>";
}


?>

I whipped it on there and it returned:

Warning: Invalid argument supplied for foreach() in

 

I don't know weather I included it in the wrong part of my script or missed something so I don't know if its what I mean...I don't know what the problem is to fix it and see.

 

I think its because I haven't pre-defined the variable $results just after 'foreach' but thats just a guess?

Hmm...I don't understand? Atleast I don't think I do.

 

So I should define my database query as a result like this:

$results = mysql_query('SELECT * FROM content');

 

Or as an array thing like this:

$query = mysql_query('SELECT * FROM contact');

$results = mysql_fetch_array($query);

 

Or am I completely off? Lol.

 

yeah you can do this

 

$query = mysql_query('SELECT * FROM contact');

 

while($results = mysql_fetch_array($query,MYSQL_ASSOC)){

    $title = $results['title'];

    $id = $results['id'];

    $content = $results['content'];

    echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>";

    echo "<div id=\"$id\" style=\"display:none\">$content</div>";

}

Cheers for your help pal but I figured a way to do it!

$query = mysql_query("SELECT * FROM content WHERE id =1") or die(mysql_error()); 
$result = mysql_fetch_array( $query );

echo $result['title'];
echo $result['content'];

 

Well same thing really isn't it?

 

What I need to do now is find a way to allow the visitor to select the id so I don't have to manually add each page...any ideas?

 

I'm thinking...create a dropbox, post it to a variable and replace the id number with the variable?...

 

Can I do that with a solid <a href=#></a> link instead of a dropdown?

 

Ta.

no thats not rite because if the id is unique your fetching one record instead of an array of records? if your only wanting one record then use mysql_fetch_row instead.

 

$query = mysql_query("SELECT * FROM content WHERE id =1") or die(mysql_error());

 

if($query){

  $row = mysql_fetch_row($query);

}

// if your table is structure like this?

$row[0]; // id

$row[1]; // title

$row[2]; // content

I don't understand?...I only want  to show one row info at a time, not all of the tables rows at the same time? (thats what your code looks like its going to do?)...have a look:

 

The code I used above works fine except I have to manually define an id number in my query.

 

The result of the code prints the page title and content onto the page just fine.

 

[site url removed]

 

Click 'Create Page' then add a title and create a simple html page in the content box.

 

The next id number called up is going to be 7 so I'll edit the display page to show the info from id=7 so you can see how it works.

 

Let me know when your done so I can remove this link as my database isn't protected because its just for me to practice on.

is this what u mean??

 

<html>
<head>
<script>

function show_content(id){
    document.getElementById(id).style.display='block';
}


</script>
</head>
</html>
<?php
// sudo code
foreach($results as $result){
    // results holding your records from your content table
    $title = $result['title'];
    $id = $result['id'];
    $content = $result['content'];
    echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>";
    echo "<div id=\"$id\" style=\"display:none\">$content</div>";
}


?>

 

Ok I tried using this method but again there a problems and I don't have a clue where to start looking in order to fix them...

 

This is what happens: Here

 

How would I be able to separate the links so it looks more like a listed navigation and also to only display one page.

 

There are no spaces between the links and all though it will display the correct page when a link is clicked it does not replace it when the other link is clicked, only adds it to the page.

 

Here's the full page code:

<html>
<head>
<script>

function show_content(id){
    document.getElementById(id).style.display='block';
}


</script>
</head>
<body>
<font face=verdana size=1>
<?php
include 'dbwire.php'; 

$query = mysql_query('SELECT * FROM content');

while($results = mysql_fetch_array($query,MYSQL_ASSOC)){
    $title = $results['title'];
    $id = $results['id'];
    $content = $results['content'];
    echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>";
    echo "<div id=\"$id\" style=\"display:none\">$content</div>";
}


?>
<hr>

</body>
</html>

 

How would I achieve what I need or atleast where would I need to look for the problem?

 

Cheers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.