3

I have a big problem. I get data from a database. I want to display them in tabs. My method is a loop in a loop. I show you the code. My problem is that the result (second loop) all data are output.

The first loop displays each user name with the same id, in a own tab. that works. the second loop operates on the same principle. but the second loop does not filter.

(!) The first loop show me correctly for every username with the same id the own Tab. but the sec on loop show me all event_names, not only the event names of the users. Resume: i got 3 user. every user. user 1 has his own tab. (2 events) user 2 has his own tab. (1 event) user 3 has his own tab. (1 event) in the second loop, i ask the events of every user. in the tab of user 1 must appear 2 events in the tab of user 2 must appear 1 event and in the tab of user 3 must appear 1 event too. BUT, in every tab (user 1,2 and 3) appear 4 events. the events of all users... is it a problem with the loop, isn't it? (!)

Can someone help me?

Here some pictures of the table and output:

My table users

enter image description here

the code:

<?php    
$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli2 = "SELECT username, premium FROM users WHERE id =  $id   ";  
$result2 = mysqli_query($db, $sqli2);   
foreach ($result2 as $row2)     
        {   
           echo '<div class="tabs">';

My table party:

enter image description here

the code:

$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '".$row2['username']."'   ";  
$result = mysqli_query($db, $sqli);    
while ($row = mysqli_fetch_assoc($result)) 
{
    echo '<h3> '. $row['unternehmen'] .' '. $row['event_name'] .' </h3>'; 

and my output: enter image description here

Where is the error? I can't believe it :-( Please help me!!!!

here the full code:

<?php


$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli2 = "SELECT username, premium FROM users WHERE id =  $id   ";  
$result2 = mysqli_query($db, $sqli2);


 foreach ($result2 as $row2) {


           echo '<div class="tabs">';
  echo '<ul>';
  echo '<li><a href="#tabs-1"> '. $row2['username'] .' </a></li>';
  echo '<li><a href="#tabs-2">Proin dolor</a></li>';
  echo '<li><a href="#tabs-3">Aenean lacinia</a></li>';
  echo '</ul>';
  echo '<div id="tabs-1">';
  echo '<p>Proin elit ac sollicitudin mi sit amet mauris. Nam elementum quam us.</p>';






  echo '</div>';
  echo '<div id="tabs-2">';




   echo '<p><div class="accordion">';


$db = mysqli_connect(DB_HOST, DB_BENUTZER, DB_password, DB_NAME);
mysqli_set_charset($db, "utf8");
$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '".$row2['username']."'   ";
$result = mysqli_query($db, $sqli);


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


    echo '<h3> '. $row['unternehmen'] .' '. $row['event_name'] .' </h3>'; 
    echo '<div> ';
    echo '<form enctype="multipart/form-data" method="post" >';
    echo '<legend>Event Daten</legend>';
    echo '<label for="event_name">Name:</label>';
    echo '<input type="text" id="event_name" name="event_name" value="'. $row['event_name'] .'" /><br />';
    echo '<label for="beginn">Beginn:</label>';
    echo '<input type="text" id="beginn" name="beginn" value=" '. $row['beginn'] .'" /><br />';
      echo '<label for="party_id" value=" '. $row['party_id'].'"> Party ID:'. $row['party_id'].' </label>';
      echo '<input type="radio" id="party_id" name="party_id" value=" '. $row['party_id'] .'" /><br />';

  echo '</fieldset>';
  echo '<input type="submit" value="Änderungen speichern" name="partyspeicher" />';
  echo '</form>';
        echo '</div>';

         }

    echo '</div>';
    echo '</p>';


    echo '</div>';
    echo '<div id="tabs-3">';
    echo '<p> Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>';
  echo '</div>';
  echo '</div>';
   }      





?>
4
  • Might be best to post the whole code. Commented Oct 10, 2014 at 8:53
  • Hello, before your $sqli, do $user = $row2['username']; , and in your $sqli variable replace $row[username] with $user and see if that changes anything. Why this? Well $row2[username] won't work since it should be $row2['username'] ... and to avoid the whole quotations problems, just create an extra variable. Commented Oct 10, 2014 at 9:00
  • no it doesn't change. i got 3 user. every user. user 1 has his own tab. (2 events) user 2 has his own tab. (1 event) user 3 has his own tab. (1 event) in the second loop, i ask the events of every user. in the tab of user 1 must appear 2 events in the tab of user 2 must appear 1 event and in the tab of user 3 must appear 1 event too. BUT, in every tab (user 1,2 and 3) appear 4 events. the events of all users... it it a problem with the loop, isn't it? Commented Oct 10, 2014 at 13:25
  • @Philip the whole code is posted now. Thanks!!!! Commented Oct 12, 2014 at 8:12

2 Answers 2

2

I'll just post this as an answer instead of a comment.

It seems your problem is directly related with your sql string:

$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '$row2[username]'   ";

Notice your $row2[username] , it should be $row2['username'] but this may cause a problem in your sql string. Create another variable called $username

$username = $row2['username'];
$sqli = "SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  '$username'   ";

And see if that changes anything.

For future reference:

I would recommend using PDO (more secure) instead of mysqli. Relatively easy to use with a lot of useful functionalities, such as bindValue() function, which would replace $username = $row2['username']; with something like

 $sqli = $db->prepare("SELECT party_id, event_name, beginn, ende, unternehmen FROM party WHERE unternehmen =  ':user'   ");
$sqli->blindValue(':user', $row2['username'], PDO::PARAM_STR);

(if $db was a PDO object) http://php.net/manual/fr/pdostatement.bindvalue.php

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

2 Comments

No its not this the problem. the problem is, that the first loop show all username, and in the second loop it make it too...
no it doesn't change. i got 3 user. every user. user 1 has his own tab. (2 events) user 2 has his own tab. (1 event) user 3 has his own tab. (1 event) in the second loop, i ask the events of every user. in the tab of user 1 must appear 2 events in the tab of user 2 must appear 1 event and in the tab of user 3 must appear 1 event too. BUT, in every tab (user 1,2 and 3) appear 4 events. the events of all users... it it a problem with the loop, isn't it?
1

your second query should be this

    $sqli = "SELECT party_id, event_name, beginn, ende, unternehmen 
FROM party WHERE unternehmen =  '".$row2['username']."'   "

you are missing single quete in username means it should be $row['username'];

always turn on the error or use mysqli_error() function they will tell what wrong.

Please check this link to sharp your knowlede http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html

2 Comments

no it doesn't change. i got 3 user. every user. user 1 has his own tab. (2 events) user 2 has his own tab. (1 event) user 3 has his own tab. (1 event) in the second loop, i ask the events of every user. in the tab of user 1 must appear 2 events in the tab of user 2 must appear 1 event and in the tab of user 3 must appear 1 event too. BUT, in every tab (user 1,2 and 3) appear 4 events. the events of all users... it it a problem with the loop, isn't it?
now i have updated my question :-) i hope now its clear. Sorry my bad english :-S

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.