7

I have this find_all() function which is written in a separate file:

public static function find_all() {
    return self::find_by_sql("SELECT * FROM ".self::$table_name);
}

It was referenced at the top of the file that includes my foreach loop:

<?php require_once("../../includes/initialize.php"); ?>
<?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?>
<?php
    $parents = UserParent::find_all();
?>

This is the foreach loop:

<?php foreach($parents as $parent): ?>
        <div class='popup-screen' id = "popup">
          <div class = "spacing">
            Do you want to delete this data?
          </div>
            <a href="list_users.php?parentNum=<?php echo $parent->parentNum; ?>"> <input type="button" value="YES" class = "popup-button"> </a>
            <input type="button" value="CANCEL" class = "popup-button" onClick = "hide();">
        </div> 
        <tr class = "tr-1">
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><img src="../<?php echo $parent->image_path(); ?>" width="100" height = "100" class = "profile-pic"/></td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';">Parent</td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo $parent->username; ?></td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo ucwords($parent->firstName); ?></td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent->parentNum; ?>';"><?php echo ucwords($parent->lastName); ?></td>
          <td onClick = "show();"><img src = "../stylesheets/images2/delete-icon.png" height="25" width="25" ></td>  
        </tr>
<?php endforeach; ?>

And this is the javascript code:

function show() 
{
    document.getElementById("popup").style.display='block';
}

function hide(){
    document.getElementById("popup").style.display='none';
}

Basically, what my code does is to create rows of information that gets its data from the foreach loop. At the end of each row is a delete icon, as illustrated by the img tag. Upon clicking the delete icon, the show() function will run (The show() function just shows the popup div, which is invisible) -- confirming if the user wants to delete his/her data or not. If the user clicks CANCEL, the window will close, as illustrated by the javascript code. If the user click YES, it is SUPPOSED to go to the link: list_users.php?parentNum=parentNum; ?> (The value of $parent->parentNum is different for each row). However, the anchor tag ALWAYS retrieves the link for the first row regardless of whether it's the third row or whatever (The links on the other td tags work, by the way). Now, my question is, how do I correctly link the YES button for each row on the popup div?

6
  • Are you declaring a value for parentNum anywhere? Commented May 22, 2013 at 2:33
  • 1
    Show the code where you assign values to $parents Commented May 22, 2013 at 2:38
  • The parentNum is inside the parent class, so there is no need of declaring it. It picks up the data from my database. The links on the td works, by the way, so the parentNum isn't really the problem. Commented May 22, 2013 at 2:39
  • Hi Aaron, welcome to SO. Commented May 22, 2013 at 2:42
  • Thanks, Steve. I sure do feel welcomed here. Commented May 22, 2013 at 2:47

2 Answers 2

4

The value of $parent->parentNum is different for each row

But in your code, it's not:

<?php echo $parent->parentNum; ?>

The value isn't incremented or changed or anything so it's the same for all rows.


Also, I'd strongly recommend using the <button> or <a> elements for this. That's what they were made for. <td>s weren't meant to be clicked. Good design practice is (almost) always worth a little extra effort.

IMO, your code should look more like

...
<td><a href='viewParent.php?n=<?php echo $n; ?>'></a></td>
...
<button name='show' value='1'>Show</button>

...where $n is from a for loop. Then at the top of the page

if (isset($_POST['show'])) { ...logic... }

to receive your 'show' event.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought so, too. I just don't know how to link the button properly.
Not sure what you mean? Make a <form method='post' action=''> element and have everything inside of it. Then, when you click the button, the page submits (reloads) with $_POST populated. The <a> tags will handle themselves and can be inside or outside of the form.
I made that post before you edited your response. Just disregard my comment. Anyway, thanks for your response, Steve. I'll do it later.
How will I be able to increment the value of parentNum? By the way, the data comes from a database. So, changing its value is not an option. What I do need is how to access the proper parentNum for each row outside the foreach loop. Is parentNum an array? If it is, how will I be able to get the values for each array index so that I may be able to put them in the link inside the popup div?
No worries, glad to help, sounds like you've got some work to do :) I'd start with researching foreach($data as $k=>$v) to iterate over your data set. The other questions will be answered as you understand this. BTW if this answer helped can you accept ;)
2
<?php 

$parentNum = 0;

foreach($parents as $parent): ?>
        <div class='popup-screen' id = "popup">
          <div class = "spacing">
            Do you want to delete this data?
          </div>
            <a href="list_users.php?parentNum=<?php echo $parent[$parentNum]; ?>"> <input type="button" value="list_users.php?parentNum=<?php echo $parent[$parentNum]; ?>" class = "popup-button"> </a>
            <input type="button" value="CANCEL" class = "popup-button" onClick = "hide();">
        </div> 
        <tr class = "tr-1">
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><img src="../<?php echo $parent->image_path(); ?>" width="100" height = "100" class = "profile-pic"/></td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';">Parent</td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><?php echo $parent[$parentNum]->username; ?></td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><?php echo ucwords($parent[$parentNum]->firstName); ?></td>
          <td onClick = "document.location = 'viewParent.php?parentNum=<?php echo $parent[$parentNum]; ?>';"><?php echo ucwords($parent[$parentNum]->lastName); ?></td>
          <td onClick = "show();"><img src = "../stylesheets/images2/delete-icon.png" height="25" width="25" ></td>  
        </tr>
<?php 

$parentNum++;

endforeach; ?>

9 Comments

How do I create an array for parentNum? And how do I loop through the array properly?
Thanks for your response. I forgot to tell that I have this function: $parents = UserParent::find_all();. And, $parent->parentNum is how you retrieve the parentNum from the userParent class. It retrieves data from the database and it isn't supposed to be initialized because the data are already available.
In that case, try it now. I removed the initialization. You still need to loop through the array. If that doesn't work, upload your find all function and where you reference it, so we can see the whole picture.
I edited my post. Check if you will be able to get the necessary information. I could provide more.
Yeah so you don't need to initialize it because like you said you already are. But you still need to loop through it. Did you try my code above?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.