A few questions

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

jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

A few questions

Post by jefffan24 »

First off I would like to say thank you before I get any help, I have been to multiple forums and nobody has been able to help me, I hope I can get some help here.

Question 1:

I have made a little feedback script for one of my website, it works great. But the owner of the website does not know how to use html, let alone how to use phpmyadmin to delete records. So I devised a little login system for them and I have a link that shows when they are logged in called Delete Feedback, what I want this link to do is delete the feedback. Right now i have not been able to accomplish this. My current fields in my database are: ID (primary key), Subject, Message. When I delete the ID in phpmyadmin it deletes the other two, so I figured I could just do this with SQL/PHP...Haven't been able to.

Also I need this to work in a for loop, here is a pic of what it looks like currently:

Image

Code: Select all

$sql = "SELECT  COUNT(*) FROM feedback";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
 
// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
 
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if
 
##########
 
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if
 
###########
 
 
 
// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;
 
 
##########
 
// get the info from the db 
$sql = "SELECT * FROM feedback ORDER BY ID DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
 
#########
 
//Number fetch array for $result
 
$row2 = mysql_fetch_array($result);
for($counter = 1; $counter <= 5; $counter += 1)
{
    
    $id = $_GET[ID];
    $delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
    echo "<a href=\"editphpfeedback.php?$delete$ \">Delete Feedback?</a><br />";
    echo "<font face='comic sans ms' size='-1'>Subject:&nbsp;<b><font color='red'>&nbsp;" . $row2['Subject'] . "</font></b>";
    echo "<br />Message:&nbsp;<b>" . $row2['Message'] . "</b><br /><br />";
    echo "<hr color='#EEEEEE' /></font>";
    echo "</form>";
    $row2 = mysql_fetch_array($result);
    
    
}
 
 
########
/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo "&nbsp; <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} //End IF
 
 
#########
 
// range of num links to show
$range = 5;
 
 
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
 
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo "&nbsp; [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo "&nbsp; <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for
 
 
 
 
   //Show next page link even if page is 1
if ($currentpage <= $totalpages - 1) {
   //get next page num
   $nextpage = $currentpage + 1;
 
   //show > to go to next page
   echo "&nbsp; <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   //show >> to go to last page
   echo "&nbsp; <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
 
Question Number 2:

I have pagination going on? (don't quite know how to word that) but the point of this is, I have 5 results shown per page, and I pull 5 records from my database. These are 2 seperate events in my script and they do not work with each other currently. What happens then is even though I don't have 5 records in the database it is still putting:

Subject:
Message:

5 times. Is there any way I can make those two work together so that the amount of times Subject, Message shows up is equal to the amount of rows in the database? I'm willing to try anything. (The code for this is above).

Question Number 3:

I want to make a Name optional field, but I don't want Name:, to show unless somebody puts something in the Name field. Is there any way to do this?

Once again thanks in advance I know this is a lot to ask from a new person on the forums.
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Hi jefffan24,

Question Number 1.

Where do you delete the ID? do you do it in your loop?

Question Number 2.

Code: Select all

$row2 = mysql_fetch_array($result);
for($counter = 1; $counter <= 5; $counter += 1)
{
   
    $id = $_GET[ID];
    $delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
    echo "<a href=\"editphpfeedback.php?$delete$ \">Delete Feedback?</a><br />";
    echo "<font face='comic sans ms' size='-1'>Subject:&nbsp;<b><font color='red'>&nbsp;" . $row2['Subject'] . "</font></b>";
    echo "<br />Message:&nbsp;<b>" . $row2['Message'] . "</b><br /><br />";
    echo "<hr color='#EEEEEE' /></font>";
    echo "</form>";
    $row2 = mysql_fetch_array($result);
   
   
}
In my experience, I think it would be better if you use while to fetch data from database resource.

Code: Select all

$id = $_GET[ID]; // I think you don't need to duplicate declaring this $id varible. It will all the same in your loop, won't it?
while( $row2 = mysql_fetch_array($result) ){
  echo "<a href=\"editphpfeedback.php?$delete$ \">Delete Feedback?</a><br />";
  echo "<font face='comic sans ms' size='-1'>Subject:&nbsp;<b><font color='red'>&nbsp;" . $row2['Subject'] . "</font></b>";
  echo "<br />Message:&nbsp;<b>" . $row2['Message'] . "</b><br /><br />";
  echo "<hr color='#EEEEEE' /></font><BR />";  
}
I don't think you need any <form> tag since you already use <a> tag to delete the feedback.

Question Number 3.

I think you can add the checking in the question number two loop.

Code: Select all

$id = $_GET[ID]; => I think you don't need to duplicate declaring this $id varible. It will all the same in your loop, won't it?
while( $row2 = mysql_fetch_array($result) ){
  echo "<a href=\"editphpfeedback.php?$delete$ \">Delete Feedback?</a><br />";
  echo "<font face='comic sans ms' size='-1'>Subject:&nbsp;<b><font color='red'>&nbsp;" . $row2['Subject'] . "</font></b>";
  echo "<br />Message:&nbsp;<b>" . $row2['Message'] . "</b><BR />";
  if ( ! empty ( $row2['name'] ) ){
    //echo the name here
  }
  echo "<br /><br />";
  echo "<hr color='#EEEEEE' /></font><BR />";  
}
Hope this can help you.
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

Question 1 again,

The way I have it right now with the foor loop is it counts how many rows there are total in the database first. Then it limits them to showing 5 at a time, making however many pages it needs to. So each of the 5 showing have 5 different ID's so from what I know of php I would assume that I would need to delete each one while in the loop. (I hope that makes sense).

As for 2 - While loop didn't work, no errors just nothing shows up (I think its the whole its not showing rows based on how many its counting problem).

and 3 same thing, no results showing :/
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Question 1 again.

Is the code that you post is editphpfeedback.php?
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

yes
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Ok. The code will be like this. It will only delete one ID once.

Code: Select all

$id = $_GET[ID]; => I think you don't need to duplicate declaring this $id varible. It will all the same in your loop, won't it?
$delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
while( $row2 = mysql_fetch_array($result) ){
  echo "<a href=\"editphpfeedback.php?ID=" . $row2['ID'] . "\">Delete Feedback?</a><br />";
  echo "<font face='comic sans ms' size='-1'>Subject:&nbsp;<b><font color='red'>&nbsp;" . $row2['Subject'] . "</font></b>";
  echo "<br />Message:&nbsp;<b>" . $row2['Message'] . "</b><BR />";
  if ( ! empty ( $row2['name'] ) ){
    //echo the name here
  }
  echo "<br /><br />";
  echo "<hr color='#EEEEEE' /></font><BR />";  
}
 
Try it.
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

Um it didn't look like it worked at first but it may have.

Ok so here is what happened...It worked sort of...Yes it deleted it, but when you clicked the link it refreshed the page and it still showed. But if you refresh the page again it doesn't show anymore. Anyway to fix this?
Last edited by jefffan24 on Mon Nov 02, 2009 9:26 am, edited 1 time in total.
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Oh right, i did a mistake. place the

Code: Select all

 
$id = $_GET[ID];
$delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
 
before you query the $result to show the data (// get the info from the db).
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

ok so that fixed that, sweet :D

Now to my other issues :/

Ok so the script finds out how many rows there are, it then breaks them up into 5 results at a time. THEN it makes the five Subject: Message: lines. Is there any way to make it so that how many times Subject: Message: is the same as the count, but still only show 5 results per page?
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Do you mean paginating?
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

Yes that is what it is doing now, it creates pages based on how many rows there are in the database. But as you can see in my first post in the pic, even where there are no rows in database it is still showing

Subject:
Message:

Now I sort of got the count to match based how many rows there are now:

Code: Select all

 
$test = "SELECT COUNT(*) FROM feedback";
$rowsperpage1 = 5;
 
 
//Number fetch array for $result
 
$row2 = mysql_fetch_array($result);
for($counter = $test; $counter <= $rowsperpage1 - 1; $counter += 1)
I've got to go to classes, I'll be back in about 3 hours so sorry if I don't reply :/
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Have you tried using the while to show your database data?
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

Yeah I have, it just comes up blank, no data shows up.
s1auk14
Forum Newbie
Posts: 15
Joined: Mon Nov 02, 2009 8:29 am

Re: A few questions

Post by s1auk14 »

Sorry for replying late. Can you show me your code that you have changed once more?
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: A few questions

Post by jefffan24 »

Ok here it is, I'll put some comments in to explain it a little better.

Code: Select all

 
//Get how many rows there are:
$sql = "SELECT  COUNT(*) FROM feedback";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
 
// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
 
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if
 
##########
 
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if
 
###########
 
 
 
// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;
 
 
 
//Gets ID and delete function stuff
$id = $_GET[ID];
$delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
 
// get the info from the db 
$sql = "SELECT * FROM feedback ORDER BY ID DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$test = mysql_query("SELECT COUNT(*) FROM feedback");
$rowsperpage1 = 5;
 
 
//Number fetch array for $result
 
$row2 = mysql_fetch_array($result);
for($counter = $test; $counter <= $rowsperpage1 - 1; $counter += 1)
{
    echo "<a href=\"editphpfeedback.php?ID=" . $row2['ID'] . "\">Delete Feedback?</a><br />";
    echo "<font face='comic sans ms' size='-1'>Subject:&nbsp;<b><font color='red'>&nbsp;" . $row2['Subject'] . "</font></b>";
    echo "<br />Message:&nbsp;<b>" . $row2['Message'] . "</b><br /><br />";
    echo "<hr color='#EEEEEE' /></font>";
    $row2 = mysql_fetch_array($result);
    
}
 
So what it does right now is if I have 6 records in the db, it is only showing 2 rows on the front and and 2 rows on the 2nd page with only 1 record in the db. Any ideas?
Post Reply