Jump to content

Recommended Posts

All i get is a link to view all partners. What I want to see is all the people in the database and how many comments each has.

Anyone?

Was on last night but still couldn't get it woking

 

<?php

$db = mysql_connect('--', '--', '-!');
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db ('-',$db);

$max_items = 5;

function displayPartners($all = 0){

global $db, $max_items;

if ($all == 0){

$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC LIMIT $max_items";
	}else{
$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC";
}


if (!$db)
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE border = \"1\" width=\"300\>\n";
$date = $row['date'];
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b>posted on $date</TD></TR>\n";
echo "<TR><TD>$partners</TD></TR>\n";

$comment_query = "SELECT count(*) FROM comment WHERE partners_id ={$row['id']}";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);

echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)";

echo "</TABLE>\n";
echo "<BR>\n";	
}
if ($all == 0){

echo "<a href =\"{$_SERVER['PHP_SELF']}?action=all\">View all Partners</a>\n";
}
}
function displayOneItem($id){
global $db;

$query = 'SELECT * FROM partners WHERE id = $id';
$result = mysql_query ($query);

if (mysql_num_rows ($result) == 0){
	echo 'No partners found ';
	return;
}
$row = mysql_fetch_assoc($result);
echo "<TABLE border =\"1\" width=\"300\>\n";
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$partners<b>/TD></TR>\n";

echo "</TABLE>\n";
echo "<BR>\n";

displayComments($id);

}
function displayComments($id){

global $db;

$query = "SELECT * FROM comment WHERE partners_id=$id";
$result = mysql_query ($query);

echo "Comments:<BR><HRwidth=\"300\">\n";

while ($row = mysql_fetch_assoc ($result)){

	echo "<TABLE border=\"1\"width=\"300\">\n";

	$name = htmlentities ($row['name']);
	echo "<TR><TD><b>by:$name</b></TD></TR>\n";

	$comment = strip_tags ($row['comment'],'<a><b><i><u>');
	$comment = nl2br ($comment);

	echo "<TR><TD>$comment</TD></TR>\n";

	echo "</TABLE>\n";
	echo "<BR>\n";
}
echo "<HR width=\"300\">";
echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\"method=POST>\n";
echo "Name: <input type=\"text\" " . "width=\"30\"name=\"name\"><BR>\n";

echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n";
echo "<input type=\"submit\"name=\"submit\" " ."value=\"Add Comment\"\n";
echo "</FORM>\n";
}
function addComment($id) {
global $db;
$query = "INSERT INTO comment " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')";
mysql_query ($query);

echo "Comment submitted<BR>\n";
echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n";
}

echo "<CENTER>\n";
switch($_GET['action']) {

	case 'show':
	displayOneItem($_GET['id']);
	break;
case 'all':
	displayPartners(1);
	break;
case 'addcomment':
	addComment($GET['id']);
	break;
default:
	displayPartners();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/235540-still-can-get-this-to-work/
Share on other sites

I don't know. That's the confusing part. I'm thinking about re-writing the whole thing because this is just not working. I could go about it another way. Does anyone know an easier way to display all the images in a directory in a list along with the number of comments they have, for the user that is?

 

such as:

 

Andrew (8)

tom(12)

 

then you can click on which one you want to look at.

How come you don't get an error?

because the if (!$db) bit i think, that says if not database connection do the while loop, because there is one it then skips the while statement

you should try

<?php
if ($db)
{
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE border = \"1\" width=\"300\>\n"
                 //blah blah
          }//close while
}//close if $db
if ($all==0)
{
//blah blah
}

What is the ultimate goal that you are trying to achieve? The others were right. Your first query which consists of an IF statement to choose between two different queries based on the variable $all never gets executed.

 

if ($all == 0){
	$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC LIMIT $max_items";
} else {
	$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC";
}

 

You are missing the mysql_query() function that executes the chosen query. You then move to try looping through the results which do not actually exist because the query was never executed so the result is empty, which should be throwing an error:

 

if (!$db) {
while ($row = mysql_fetch_assoc($result)){
	// This result set is empty and the mysql_fetch_assoc() function should be throwing an error if display_errors is turned on
}
}

 

So, what is the overall goal here?

The goal is to just have all the image names in the db display with the number of comments each has:

 

Tom(12)

Mike(9)...

 

And you can click which one you want to look at.

 

You are ignoring the solutions provided. Your code is all over the place, the "!" exclamation mark means the opposite of the function result. IF the database does not connect, then DO NOT run the while loop. Use your frickin brackets...

 

and this

 

($all = 0){

 

completely useless

 

What is that function even supose to return?

 

Why is your max_item and db globalized?

 

Wheres $result coming from?

 

Some parsing problems here you should fix

 

echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)";

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.