/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] If/Else Issues

OK, the following code is in a foreach loop, and I can not figure out why it isn’t working. Basically all it does is build a string to pass through the URL to the next page.

The string to be created should look like this:
imageID=148&imageID2=149
But looks like this:
imageID=148&imageID2=148

Notice in the second one the 2 numbers are the same, where in the first one they are different. My code creates the result of the second string, but I would rather the first string. Why is it doing this?

[code=php]
$twoUP = FALSE;
foreach ($_FILES[“file”][“error”] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
// upload to server and create thumbs
mysql_query(“INSERT INTO guest_images (`directory`,`file`) VALUES (‘$savelocation’,’$saveFile’)”);
if(!$twoUP){
$imID1 = ‘?imageID=’.mysql_insert_id();
if(empty($_FILES[‘file’][‘name’][1])){
break;
}else{
$twoUp = TRUE;
}
}
if($twoUp){
$imID2 = ‘&imageID2=’.mysql_insert_id();
}else{
$imID2 = ”;
}
}
}
header(“Location: guestImage.php”.$imID1.$imID2);
exit;
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@TheRaveFeb 13.2008 — [code=php]$imID1 = '?imageID='.mysql_insert_id();
//**snip**
$imID2 = '?imageID='.mysql_insert_id();
[/code]


During the snip no further SQL insert is executed so the insert_id will be the same.

I'm guessing what you're trying to do you need an elseif instead of an if:
[code=php]elseif($twoUp){[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorFeb 13.2008 — huh?

I execute the SQL two times, why would it be the same?
Copy linkTweet thisAlerts:
@TheRaveFeb 13.2008 — Depends on if the $_FILES array has two files. If it doesn't then you're not.

TBH it would make more sense to construct an array of $imIDs then just pass the array. It would save complex logic (ok not really complex but more than you need) required to have two seperate variables within the loop.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorFeb 13.2008 — No, It then only displays:

imageID=148

with the elseif
Copy linkTweet thisAlerts:
@TheRaveFeb 13.2008 — Which proves that the loop is only executing once. If it executed twice then the second time $twoUp would be true and it would set the second ID.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorFeb 13.2008 — No it is executing twice, because it creates both of the images successfully, and saves them to the database successfully, it is just generating an incorrect URL.
Copy linkTweet thisAlerts:
@TheRaveFeb 13.2008 — So you can understand the logic of your code I would suggest either:

  • - Stepping through it during execution (many tools allow you to do this, e.g. phpeclipse or phpdesigner)

  • - Temporarily removing/commenting out the location header and echoing out every variable at every stage of the loop.


  • This way you can see what is executing when and what is being set when.
    Copy linkTweet thisAlerts:
    @The_Little_GuyauthorFeb 13.2008 — OK, I found it is going into this if section two times.
    [code=php]
    if(!$twoUP){
    $imID1 = '?imageID='.mysql_insert_id();
    if(empty($_FILES['file']['name'][1])){
    break;
    }else{
    $twoUp = TRUE;
    }
    }
    [/code]
    ×

    Success!

    Help @The_Little_Guy spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...