14 Replies - 739 Views - Last Post: 09 May 2011 - 08:57 AM Rate Topic: -----

#1 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

If statement issues

Posted 07 May 2011 - 11:11 AM

After it goes through the while loop its supposed to take the ID of the first one in the loop and find out its awardType and then with the awardType variable go through the the if statement. After it displays it the code that fits the awardType variable then it gets the second item in the while loop and then with the ID of the second item takes it back to get the awardType and then goes back through the if statement again. I'm not sure what I'm doing wrong. As of right now all its doing is displaying its awardName and a blank dropdown.

What the rendered is that it seems to always be putting in the else part of the statement for each award and that's not right because most of them should be using the first part of the if statement.


<?php

session_start(); // Access the existing session

// Include the database page
require ('../../inc/dbconfig.php');l

$userID = $_SESSION['userID'];

$statusQuery = "
    SELECT 
        * 
    FROM 
        statuses";
$statusResult = mysqli_query ( $dbc, $statusQuery ); // Run The Query 

$awardQuery = "
    SELECT
        awards.ID,
        awards.awardName,
        awards.awardType
    FROM
        awards";
$awardResult = mysqli_query ( $dbc, $awardQuery ); // Run The Query
$row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC );

$awardType = $row[ 'awardType' ];

$charactersQuery = "
    SELECT 
        characters.ID, 
        characters.characterName 
    FROM 
        characters
    ORDER BY 
        characters.characterName";
$charactersResult = mysqli_query ( $dbc, $charactersQuery ); // Run The Query

$matchQuery = "
    SELECT
        eventSegments.ID,
        eventSegments.segmentTitle
    FROM
        eventSegments
        INNER JOIN events
            ON eventSegments.eventID = events.ID
    WHERE
        DATE_FORMAT(events.bookingDate,'%Y') = DATE_FORMAT(curdate(),'%Y')";
$matchResult = mysqli_query ( $dbc, $matchQuery ); // Run The Query


?>



<fieldset>
		<legend>Nominees</legend>
        <?php 
        mysqli_data_seek( $awardResult, 0 );
        while ( $row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC ) ) { ?>
        <?php if ($awardType == 'Singular') { ?>
        <div class="field required">
			<label for="charactersDrop"><?php echo $row['awardName']; ?></label>
			<select class="dropdown" name="charactersDrop" id="charactersDrop" title="Characters Drop">
                <option value="">- Select -</option>
               <?php
                    while ( $row = mysqli_fetch_array ( $charactersResult, MYSQL_ASSOC ) ) { 
                        print "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";
                    }
                ?>
            </select>
            <input type="button" value="Add Character" class="" onclick="HandlerCharacters()"/>
            <ul id="characterList">
            </ul>
            <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
        </div>
        <?php } else { ?>
        <div class="field required">
			<label for="events"><?php echo $row['awardName']; ?></label>
			<select class="dropdown" name="events" id="events" title="Events for <?php echo date("Y") ?>">
                <option value="">- Select -</option>
               <?php
                    while ( $row = mysqli_fetch_array ( $matchResult, MYSQL_ASSOC ) ) { 
                        print "<option value=\"".$row['ID']."\">".$row['segmentTitle']."</option>\r";
                    }
                ?>
            </select>
            <input type="button" value="Add Match" class="" onclick="AddMatch()"/>
            <ul id="matchList">
            </ul>
            <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
        </div>
        <?php } ?>
        <?php } ?>
</fieldset>

This post has been edited by xtremer360: 07 May 2011 - 11:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: If statement issues

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: If statement issues

Posted 07 May 2011 - 02:08 PM

Well, if we remove all the extraneous junk (that PHP will completely ignore, namely HTML) we see this:
mysqli_data_seek( $awardResult, 0 );
while ( $row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC ) ) { 
	if ($awardType == 'Singular') { 
		echo $row['awardName'];
		while ( $row = mysqli_fetch_array ( $charactersResult, MYSQL_ASSOC ) ) { 
			print "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";
		}
	} else {
		while ( $row = mysqli_fetch_array ( $matchResult, MYSQL_ASSOC ) ) { 
			print "<option value=\"".$row['ID']."\">".$row['segmentTitle']."</option>\r";
		}
	}
}


Now that we can see the logic it becomes clear that you're changing the value of $row in the middle of the outer while loop. That can't be what you intended!
I suggest you separate your code so that you have a better chance of being able to read it!
Was This Post Helpful? 3
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: If statement issues

Posted 07 May 2011 - 02:13 PM

Just a quick tip, don't mix PHP and HTML. It's very messy and makes it hard to figure out what the problem is.
Was This Post Helpful? 1
  • +
  • -

#4 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 07 May 2011 - 02:19 PM

So your saying if I call the second $row variable like $row2?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: If statement issues

Posted 07 May 2011 - 02:39 PM

That would probably help, but you're not doing yourself any favors by keeping all that HTML in with the PHP. Look at post #2. It only has 13 lines of code! If that's enough to confuse you, then you need to rethink how you're writing code. Read the link I provided.
Was This Post Helpful? 1
  • +
  • -

#6 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 08 May 2011 - 09:59 AM

That didn't correct the issue.
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: If statement issues

Posted 08 May 2011 - 10:35 AM

Post your up-to-date code.
Was This Post Helpful? 0
  • +
  • -

#8 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 08 May 2011 - 10:37 AM

mysqli_data_seek( $awardResult, 0 );
while ( $row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC ) ) { 
	if ($awardType == 'Singular') { 
		echo $row['awardName'];
		while ( $row2 = mysqli_fetch_array ( $charactersResult, MYSQL_ASSOC ) ) { 
			print "<option value=\"".$row2['ID']."\">".$row2['characterName']."</option>\r";
		}
	} else {
		while ( $row2 = mysqli_fetch_array ( $matchResult, MYSQL_ASSOC ) ) { 
			print "<option value=\"".$row2['ID']."\">".$row2['segmentTitle']."</option>\r";
		}
	}
}




Was This Post Helpful? 0
  • +
  • -

#9 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: If statement issues

Posted 08 May 2011 - 11:01 AM

To debug your code you're going to need to dump the following variables and make sure they are what they're suppose to be.
  • $awardResult
  • $row
  • $awardType
  • $row2


You can dump variables using var_dump.

var_dump($variable1, $variable2, $variable3); //Example code


Also place this at the top of your script to see all errors.
error_reporting(E_ALL);

Was This Post Helpful? 1
  • +
  • -

#10 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 08 May 2011 - 11:17 AM

I'm assuming its giong to be important where I place the var dump huh?
Was This Post Helpful? 0
  • +
  • -

#11 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 08 May 2011 - 11:31 AM

I put on my form page and put <pre></pre> tags around it and it returned this:

object(mysqli_result)#3 (0) {
}
array(3) {
  ["ID"]=>
  string(1) "1"
  ["awardName"]=>
  string(17) "Match of the Year"
  ["awardType"]=>
  string(5) "Match"
}
NULL
string(5) "Match"

This post has been edited by JackOfAllTrades: 08 May 2011 - 01:12 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: If statement issues

Posted 08 May 2011 - 12:05 PM

The best place to insert var_dump is just before the line in which the variable is used.

What we really need here is $row and $row2. Placing one after the 2 while statements is where u want it. Hopefully your loop isn't extremely long.
Was This Post Helpful? 1
  • +
  • -

#13 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 09 May 2011 - 08:15 AM

So the following was produced when I did that however even though I'm looking at what the var_dump function came up with it doesn't apply it to the dropdowns with the if statement loop. So when it goes back through it doesn't change the awardType variable.

array(3) { ["ID"]=> string(1) "1" ["awardName"]=> string(17) "Match of the Year" ["awardType"]=> string(5) "Match" }

array(3) { ["ID"]=> string(1) "2" ["awardName"]=> string(22) "Best Singles Performer" ["awardType"]=> string(9) "Character" }

array(3) { ["ID"]=> string(1) "3" ["awardName"]=> string(24) "Best Manager of the Year" ["awardType"]=> string(9) "Character" }

array(3) { ["ID"]=> string(1) "4" ["awardName"]=> string(22) "Best Woman of the Year" ["awardType"]=> string(9) "Character" }

array(3) { ["ID"]=> string(1) "5" ["awardName"]=> string(13) "Best Finisher" ["awardType"]=> string(4) "Move" }

array(3) { ["ID"]=> string(1) "6" ["awardName"]=> string(40) "Best New Sensation of the Squared Circle" ["awardType"]=> string(9) "Character" }

array(3) { ["ID"]=> string(1) "7" ["awardName"]=> string(25) "Best Tag Team of the Year" ["awardType"]=> string(9) "Character" }




<?php require ('php/awardsshow.php'); ?>
    
<script type="text/javascript" src="forms/addnew/js/awardsshow.js"></script>

<!-- Form -->
<form action="#" id="awardsshowForm" >
	<fieldset>
		<legend>Add New Awards Show</legend>
        <div class="field required">
			<label for="date">Date</label>
			<input type="text" class="text" name="date" id="date" title="Date" readonly="readonly"/> 
            <span style="margin-left: -36px;" class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
            <div id="datepicker"></div>
        </div>
        <div class="field required">
			<label for="statusID">Status</label>
            <select class="dropdown" name="statusID" id="statusID" title="Status">
                <option value="">- Select -</option>
                <?php
                    while ( $row = mysqli_fetch_array ( $statusResult, MYSQL_ASSOC ) ) { 
                        print "<option value=\"".$row['ID']."\">".$row['statusName']."</option>\r";
                    }
                ?>
            </select>
			<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
		</div>
	</fieldset>
    <fieldset>
		<legend>Nominees</legend>
        <?php 
        mysqli_data_seek( $awardResult, 0 );
        while ( $row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC ) ) { ?>
        <?php var_dump($row); //Example code ?>
        <?php if ($awardType == 'Singular') { ?>
        <div class="field required">
			<label for="charactersDrop"><?php echo $row['awardName']; ?></label>
			<select class="dropdown" name="charactersDrop" id="charactersDrop" title="Characters Drop">
                <option value="">- Select -</option>
               <?php
                    while ( $row2 = mysqli_fetch_array ( $charactersResult, MYSQL_ASSOC ) ) { 
                        var_dump($row2); //Example code
                        print "<option value=\"".$row2['ID']."\">".$row2['characterName']."</option>\r";
                    }
                ?>
            </select>
            <input type="button" value="Add Character" class="" onclick="HandlerCharacters()"/>
            <ul id="characterList">
            </ul>
            <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
        </div>
        <?php } elseif ($awardType == 'Match') { ?>
        <div class="field required">
			<label for="events"><?php echo $row['awardName']; ?></label>
			<select class="dropdown" name="events" id="events" title="Events for <?php echo date("Y") ?>">
                <option value="">- Select -</option>
               <?php
                    while ( $row2 = mysqli_fetch_array ( $matchResult, MYSQL_ASSOC ) ) { 
                        var_dump($row2); //Example code
                        print "<option value=\"".$row2['ID']."\">".$row2['segmentTitle']."</option>\r";
                    }
                ?>
            </select>
            <input type="button" value="Add Match" class="" onclick="AddMatch()"/>
            <ul id="matchList">
            </ul>
            <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
        </div>
        <?php } else { ?>
        <div class="field required">
			<label for="moves"><?php echo $row['awardName']; ?></label>
			<select class="dropdown" name="moves" id="moves" title="Finishing Moves">
                <option value="">- Select -</option>
               <?php
                    while ( $row2 = mysqli_fetch_array ( $moveResult, MYSQL_ASSOC ) ) 
                    var_dump($row2); //Example code
                    { 
                        print "<option value=\"".$row2['ID']."\">".$row2['finisher']."</option>\r";
                    }
                ?>
            </select>
            <input type="button" value="Add Move" class="" onclick="AddMMove()"/>
            <ul id="matchList">
            </ul>
            <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
        </div>
        <?php } ?>
        <?php } ?>
	</fieldset>
    <fieldset>
    <input type="hidden" name="userID" id="userID" value="<?php echo $userID; ?>" />  
    <input type="submit" class="submit" name="submitAwardsShow" id="submitAwardsShow" title="Submit Awards Show" value="Submit Awards Show"/>
    </fieldset>
</form>
<!-- /Form -->

<!-- Messages -->
<div class="message message-error">
    <h6>Required field missing</h6>
    <p>Please fill in all required fields. </p>
</div>
<div class="message message-success">
    <h6>Operation succesful</h6>
    <p>Arena was added to the database.</p>
</div>
<!-- /Messages -->

Was This Post Helpful? 0
  • +
  • -

#14 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: If statement issues

Posted 09 May 2011 - 08:44 AM

The $awardType variable won't change because it's not assigned in the while loop. I remember seeing it being assigned further up in your code. Either put $awardType = $row['awardType'] in the first while loop or just test it directly from the row array like if($row['awardType'] == 'Singular') {
Was This Post Helpful? 1
  • +
  • -

#15 xtremer360  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 123
  • Joined: 03-March 11

Re: If statement issues

Posted 09 May 2011 - 08:57 AM

My issue is solved. Thank you for all your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1