Jump to content

mysql/php error


shinigaimi
Go to solution Solved by shinigaimi,

Recommended Posts

Hey all I am trying to filter if a variable doesnt exist in mysql to output something right now it does nothing :( any ideas?

 

here is the code in question, I have tried !isset(), is_null(), empty() I am baffled! :( I know that array key #10/11 are not in mysql and they do not display anything I am trying to make it so if the $vehicle doesnt exist to do something :(

 

the part where I am having issues is on line 32 the if statement

 

Thanks!

			$array = array(
				"1"  => "Flash",
				"2"  => "Sunderer",
				"3"  => "Lightning",
				"6"  => "Prowler",
				"9"  => "Mosquito",
				"10" => "Liberator",
				"11" => "Galaxy",
				"12" => "Harasser",
			);
foreach ($array as $key => $value) {
		$get = $dbh_beta->query("SELECT * FROM weapon_kills WHERE character_number=" . $char_id . " AND vehicle=" . $key. "");
		$get->setFetchMode(PDO::FETCH_ASSOC);
		while ($display = $get->fetch()) {
			$vehicle  = $display["vehicle"];
			$value_nc = $display["value_nc"];
			$value_tr = $display["value_tr"];
			$value_vs = $display["value_vs"];
			$total    = $value_nc + $value_tr + $value_vs;
			?>
			<?php
			if ($faction === "nc") {
				?>
				<td><?php echo number_format($value_tr + $value_vs); ?></td>
				<td><?php echo number_format($value_tr); ?></td>
				<td><?php echo number_format($value_vs); ?></td>
			<?php
			} elseif ($faction === "tr") {
				?>
				<td><?php
					echo number_format($value_nc + $value_vs);
					if(!$vehicle){
						echo "-";
					}
					?>
				</td>
				<td><?php echo number_format($value_nc); ?></td>
				<td><?php echo number_format($value_vs); ?></td>
			<?php
			} elseif ($faction === "vs") {
				?>
				<td><?php echo number_format($value_nc + $value_tr); ?></td>
				<td><?php echo number_format($value_nc); ?></td>
				<td><?php echo number_format($value_tr); ?></td>
			<?php
			}
			break;
		}
}
Edited by shinigaimi
Link to comment
Share on other sites

if there is no row for a vehicle, there's nothing for your while(){} loop to loop over and the code inside your while loop doesn't even run. also, since there's no row, there are no values for $value_nc, $value_tr, and $value_vs and trying to access them would produce errors. what do you want the output to be in this case? a message, just zeros for those values,...?

 

however, you should never run SELECT queries inside of loops. you should run ONE query that gets the rows you want.

 

the following example code (untested) queries for the rows, then loops over the values in the $array and outputs whatever you want when there is no row or the data from the query if there is a row -

$keys = implode(',',array_keys($array)); // get the keys as a list to query for
$query = "SELECT * FROM weapon_kills WHERE character_number=$char_id AND vehicle IN($keys)";
$data = array();
foreach($dbh_beta->query($query,PDO::FETCH_ASSOC) as $row){
    $data[$row['vehicle']] = $row; // store each row using the vehicle as the key/index
}

foreach ($array as $key => $value){
    if(!isset($data[$key])){
        // no vehicle, output whatever you want for that case...
        echo "<td colspan='3'>The vehicle $key:$value doesn't exist.</td>";
    } else {
        // the vehicle exists in the table
        $row = $data[$key];
        $value_nc = $row["value_nc"];
        $value_tr = $row["value_tr"];
        $value_vs = $row["value_vs"];
        if($faction === "nc"){
            echo "<td>".number_format($value_tr + $value_vs)."</td>
            <td>".number_format($value_tr)."</td>
            <td>".number_format($value_vs)."</td>
            ";
        } elseif ($faction === "tr"){
            echo "<td>".number_format($value_nc + $value_vs)."</td>
            <td>".number_format($value_nc)."</td>
            <td>".number_format($value_vs)."</td>
            ";
        } elseif ($faction === "vs"){
            echo "<td>".number_format($value_nc + $value_tr)."</td>
            <td>".number_format($value_nc)."</td>
            <td>".number_format($value_tr)."</td>
            ";
        }
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.