Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2018
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looping Thru a Table - only showing 1 entry PHP

    I have this code i created and there are 2 entries in the Database Table however on the main page it is only showing 1 entry the PHP script is pretty complicated and involves using a few While Loops thru a number of DB tables but looking for help to get a limit of 5 transactions showing on the main page

    this is the data being used to output the players, draftpicks (d), goalies (g), and prospects (p)

    -screenshot_14-png

    Code is as follows:

    echo '<h2>RECENT TRANSACTIONS:</h2>';

    $stmt = $conn->prepare("SELECT Team1, Team2, players1, players2, Details FROM transactions");
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    while($rows = $result->fetch_assoc()) {
    $team1 = $rows['Team1'];
    $players1 = $rows['players1'];
    $details = $rows['Details'];
    $tradearray = explode(",", $players1);
    $trade = array($tradearray);

    $stmt3 = $conn->prepare("SELECT abbre FROM teams WHERE number=?");
    $stmt3->bind_param("i", $team1);
    $stmt3->execute();
    //fetching result would go here, but will be covered later
    $result2 = $stmt3->get_result();
    while($row3 = $result2->fetch_assoc()) {

    $team_1 = $row3['abbre'];
    }

    for ($i=0; $i < count($trade); $i++){

    foreach ($trade[$i] as $line) {

    if (stripos($line, "p") !== false) {

    $prospects = (intval(trim($line, "p")));



    $stmt = $conn->prepare("SELECT Player FROM prospects WHERE id=?");
    $stmt->bind_param("i", $prospects);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    while($rows = $result->fetch_assoc()) {

    $val = $rows['Player'];

    $spect = array($val);



    }

    } elseif (stripos($line, "d") !== false){

    $draftpicks = (intval(trim($line, "d")));

    $stmt = $conn->prepare("SELECT Own_by_Team, Round, Year FROM draftpicks WHERE id=?");
    $stmt->bind_param("i", $draftpicks);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    $counter = 0;
    while($rows = $result->fetch_assoc()) {
    $team2 = $rows['Own_by_Team'];

    $div = $conn->prepare("SELECT abbre FROM teams WHERE number = ?");
    $div->bind_param("i", $rows['Own_by_Team']);
    $div->execute();
    //fetching result would go here, but will be covered later
    $result1 = $div->get_result();
    while($row1 = $result1->fetch_assoc()) {
    $pick1ownedby = $row1['Own_by_Team'];
    $abbre = $row1['abbre'];

    if ($rows['Round'] == 1) {

    $draft1 = array('First Round Pick');

    } elseif ($rows['Round'] == 2) {

    $draft2 = array('Second Round Pick');

    } elseif ($rows['Round'] == 3) {

    $draft3 = array('Third Round Pick');

    }



    }
    }

    } elseif (stripos($line, "g") !== false) {

    $goalies = (intval(trim($line, "g")));



    $stmt = $conn->prepare("SELECT Name FROM goalies WHERE id=?");
    $stmt->bind_param("i", $goalies);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    while($rows = $result->fetch_assoc()) {

    $goaliename = $rows['Name'];

    $goalie = array($goaliename);


    }

    } else {

    $stmt = $conn->prepare("SELECT Name FROM players WHERE Number=?");
    $stmt->bind_param("i", $line);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    $counter = 0;
    while($rows = $result->fetch_assoc()) {

    $name = $rows['Name'];

    $player = array($name);

    }

    }



    }

    }

    echo '<table id="transactions"><tr><td><a href='.$team_1.'.php" target="_blank"><img src="images/'.$team_1.'.gif"></a></td><td>';

    $spectlength = count($spect);

    for($x = 0; $x < $spectlength; $x++) {
    echo $spect[$x];
    if ($spect[$x] <> '') {
    echo "<br>";
    }
    }

    $draft1length = count($draft1);

    for($x = 0; $x < $draft1length; $x++) {
    echo '<img src="images/'.$abbre.'.gif"></a>'; echo $draft1[$x];
    if ($draft1[$x] <> '') {
    echo '<br>';
    }
    }

    $draft2length = count($draft2);

    for($x = 0; $x < $draft2length; $x++) {
    echo '<img src="images/'.$abbre.'.gif"></a>'; echo $draft2[$x];
    if ($draft2[$x] <> '') {
    echo '<br>';
    }
    }

    $draft3length = count($draft3);

    for($x = 0; $x < $draft3length; $x++) {
    echo '<img src="images/'.$abbre.'.gif"></a>'; echo $draft3[$x];
    if ($draft3[$x] <> '') {
    echo '<br>';
    }
    }

    $playerlength = count($player);

    for($x = 0; $x < $playerlength; $x++) {
    echo $player[$x];
    if ($player[$x] <> '') {
    echo '<br>';
    }
    }
    $goalielength = count($goalie);

    for($x = 0; $x < $goalielength; $x++) {
    echo $goalie[$x];
    if ($goalie[$x] <> '') {
    echo '<br>';
    }
    }
    }

    $stmt = $conn->prepare("SELECT Team1, Team2, players1, players2, Details FROM transactions");
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    while($rows = $result->fetch_assoc()) {
    $players2 = $rows['players2'];
    $team2 = $rows['Team2'];
    $trade1array = explode(",", $players2);
    $trade1 = array($trade1array);

    $stmt1 = $conn->prepare("SELECT abbre FROM teams WHERE number=?");
    $stmt1->bind_param("i", $team2);
    $stmt1->execute();
    //fetching result would go here, but will be covered later
    $result1 = $stmt1->get_result();
    while($rows1 = $result1->fetch_assoc()) {

    $team_2 = $rows1['abbre'];
    }

    for ($i=0; $i < count($trade1); $i++){

    foreach ($trade1[$i] as $line) {

    if (stripos($line, "p") !== false) {

    $prospects1 = (intval(trim($line, "p")));



    $stmt = $conn->prepare("SELECT Player FROM prospects WHERE id=?");
    $stmt->bind_param("i", $prospects1);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    while($rows = $result->fetch_assoc()) {

    $val1 = $rows['Player'];

    $spect1 = array($val1);
    // and add two more values to the begin of the fruit array


    }

    } elseif (stripos($line, "d") !== false){

    $draftpicks1 = (intval(trim($line, "d")));

    $stmt = $conn->prepare("SELECT Own_by_Team, Round, Year FROM draftpicks WHERE id=?");
    $stmt->bind_param("i", $draftpicks1);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    $counter = 0;
    while($rows = $result->fetch_assoc()) {
    $pickownedby = $rows['Own_by_Team'];
    $div = $conn->prepare("SELECT abbre FROM teams WHERE number = ?");
    $div->bind_param("i", $rows['Own_by_Team']);
    $div->execute();
    //fetching result would go here, but will be covered later
    $result1 = $div->get_result();
    while($row1 = $result1->fetch_assoc()) {
    $abbre1 = $row1['abbre'];

    if ($rows['Round'] == 1) {

    $draft4 = array('First Round Pick');

    } elseif ($rows['Round'] == 2) {

    $draft5 = array('Second Round Pick');

    } elseif ($rows['Round'] == 3) {

    $draft6 = array('Third Round Pick');

    }



    }
    }

    } elseif (stripos($line, "g") !== false) {

    $goalies1 = (intval(trim($line, "g")));



    $stmt = $conn->prepare("SELECT Name FROM goalies WHERE id=?");
    $stmt->bind_param("i", $goalies1);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    while($rows = $result->fetch_assoc()) {

    $goaliename1 = $rows['Name'];

    $goalie1 = array($goaliename1);


    }

    } else {

    $stmt = $conn->prepare("SELECT Name FROM players WHERE Number=?");
    $stmt->bind_param("i", $line);
    $stmt->execute();
    //fetching result would go here, but will be covered later
    $result = $stmt->get_result();
    $counter = 0;
    while($rows = $result->fetch_assoc()) {

    $name1 = $rows['Name'];

    $player1 = array($name1);

    }

    }



    }

    }

    echo '</td><td><a href='.$team_2.'.php target="_blank"><img src="images/'.$team_2.'.gif"></a></td><td>';

    $spect1length = count($spect1);

    for($x = 0; $x < $spect1length; $x++) {
    echo $spect1[$x];
    if ($spect1[$x] <> '') {
    echo "<br>";
    }
    }

    $draft4length = count($draft4);

    for($x = 0; $x < $draft4length; $x++) {
    echo '<img src="images/'.$abbre1.'.gif"></a>'; echo $draft4[$x];
    if ($draft4[$x] <> '') {
    echo '<br>';
    }
    }

    $draft5length = count($draft5);

    for($x = 0; $x < $draft5length; $x++) {
    echo '<img src="images/'.$abbre1.'.gif"></a>'; echo $draft5[$x];
    if ($draft5[$x] <> '') {
    echo '<br>';
    }
    }

    $draft6length = count($draft6);

    for($x = 0; $x < $draft6length; $x++) {
    echo '<img src="images/'.$abbre1.'.gif"></a>'; echo $draft6[$x];
    if ($draft6[$x] <> '') {
    echo '<br>';
    }
    }

    $player1length = count($player1);

    for($x = 0; $x < $player1length; $x++) {
    echo $player1[$x];
    if ($player1[$x] <> '') {
    echo '<br>';
    }
    }

    $goalie1length = count($goalie1);

    for($x = 0; $x < $goalie1length; $x++) {
    echo $goalie1[$x];
    if ($goalie1[$x] <> '') {
    echo '<br>';
    }
    }

    echo '</td></tr><tr><td colspan=4>'.$details.'</td></tr>';
    }


    echo'</table>';

    $stmt->close();

    ?>

    I know the majority of code works but am confused why its showing only one transaction as follows:

    -screenshot_15-png

    Any help would be appreciated thanks

    If you need to know where some of the {} starts and ends i can forward that in a message

    Thanks

  2. #2
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    272
    Thanks
    3
    Thanked 19 Times in 18 Posts
    Without indentation, it's difficult to tell, you should post post your code inside PHP tags so we can see indentation.

    I imagine that using the same variable names over could be an issue, but can't tell because it's extremely difficult to see where control structures begin and end with no indentation.

    If you want help, you should edit that post and preserve the indentation so we can follow your code.

  3. #3
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,955
    Thanks
    36
    Thanked 1,064 Times in 1,060 Posts
    PHP tags are [code] with closing tag or for this section use [PHP] to enclose your code.

    You have this:
    PHP Code:
    for($x 0$x $draft1length$x++) {
        echo 
    '<img src="https://www.codingforums.com/images/'.$abbre.'.gif"></a>'; echo $draft1[$x];   //NO LINE FEED So your over writing things and only the last value is shown. $draft1 is in 6 places in your code. Good Luck.
        
    if ($draft1[$x] <> '') {
            echo 
    '<br>';
        }

    Last edited by sunfighter; Feb 7th, 2019 at 12:24 AM.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  4. #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,257
    Thanks
    3
    Thanked 553 Times in 538 Posts
    Sorry for how this sounds, but this code is a wall of unnecessary code, queries, variables, and loops. There's 380+ posted lines of code. This can be accomplished with one query and probably about 20 lines of code.

    Some of the problems -
    1. The data is not normalized and is not properly stored.
    2. No useful comments in the code.
    3. Using a prepared query when there is no external data being supplied to the query.
    4. Running queries inside of loops.
    5. Creating a large number of unnecessary variables that are copies of other variables.
    6. Creating variables that are not used.
    7. Using loops to fetch a single row of data.
    8. Creating arrays around single values, only to add an extra loop to access the original value.
    9. Using numbered variables when there is only a single value at a time, then writing three sets of code to display a single value.
    10. Reusing variables before finishing with their previous usage.

    Item #10 is the reason you are getting only one result.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  5. #5
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,955
    Thanks
    36
    Thanked 1,064 Times in 1,060 Posts
    Wow CFMaBiSmAd I'm impressed that you read that much of the OPs code. I just looked for the normal reason for that error. I figured that this could be shrunk into a manageable length by someone with the time and wherewithal.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •