Jump to content

Archived

This topic is now archived and is closed to further replies.

EriRyoutan

Infinite OR's?

Recommended Posts

Okay. What I'm trying to do is come up with a way to make an infinite (?) number of OR's in an if statement.

ex:[code]if($a == $a2 || $b == $b2 || $c == $c2 [...])[/code]

I was thinking about using arrays, but that seems like it would end up being like an AND statement.[code]if(array1[] == array2[])[/code]

Hm... any ideas?

Thanks for reading. ^^

Share this post


Link to post
Share on other sites
so, are you trying to say, as long an one match exists, do it, but if no matches, then don't? Infinite is a big number, how many are there?

Share this post


Link to post
Share on other sites
What I'm trying to do is check a big list of things. ... The problem is, the number of things in the list changes.

sometimes it'll be as short as [code]if($a == $a2 || $b == $b2)[/code]

Sometimes it'll have ten elements, sometimes up to like twenty. (I hope not ><; )

I'm trying to keep it dynamic, tho. AKA, if I only put in 20 spots, and I try and use 21 later...


(sample testing)[code]if( $_POST["oldr0c0"] != $_POST["r0c0"]
||  $_POST["oldr0c1"] != $_POST["r0c1"]
||  $_POST["oldr0c2"] != $_POST["r0c2"]
||  $_POST["oldr0c3"] != $_POST["r0c3"]
||  $_POST["oldr0c4"] != $_POST["r0c4"]
||  $_POST["oldr0c5"] != $_POST["r0c5"]
) {echo "Do Stuff...";}
if( $_POST["oldr1c0"] != $_POST["r1c0"]
||  $_POST["oldr1c1"] != $_POST["r1c1"]
||  $_POST["oldr1c2"] != $_POST["r1c2"]
||  $_POST["oldr1c3"] != $_POST["r1c3"]
||  $_POST["oldr1c4"] != $_POST["r1c4"]
||  $_POST["oldr1c5"] != $_POST["r1c5"]
) {echo "Do Other Stuff...";}[/code]

I've already got the old and the new set up, but that way, if ANY of it changes, then it will do it. but if it's ALL the same, it just stays...

Share this post


Link to post
Share on other sites
I think its may useful!! It's just my idea to how make your problem work.. refine its well..

-- Little edit: they will be ok because of 2=2, change it to test ;)

Enjoy, D.Soul

[code]<?php
// list of array field
    $arr_a = array ( '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' );
// list of array values
    $arr_b = array ( '7' , '0' , '2' , '2' , '3' , '4' , '5' , '6' );
// check how much they are, infinites or not
    $limit = count ( $arr_a );
// variables.. start it..
    $test  = FALSE;
// for each field with each own value
    for ( $counter = 0; $counter < $limit; $counter ++ )
    {
        if ( strtolower ( $arr_a[$counter] ) == strtolower ( $arr_b[$counter] ))
        {
// test if its true.. they match??
            $test = TRUE;
        }
    }
// Now, your code.. they match? join, dont mach? put a else there..
    if ( $test )
    {
        print 'Ok! =)';
    }
?>[/code]

Share this post


Link to post
Share on other sites
You should be able to redesign your form so that the data is returned in arrays. Then if your old data is also contained in arrays, you should be able to do something like the following:
[code]<?php
$rc = $_POST['rc];
for ($c=0;$c<count($rc);$c++) {
     $ok = false;
     $row = 0;
     while ($row <count($rc[$c]) && !ok) {
           if ($rc[$c][$row] == $oldrc[$c][$row]) $ok = true;
           $row++;
     }
     if ($ok) {
//
//     do something
//
    }
}
?>[/code]

If you post the code that generates the form, we can suggest how it can be changed to return the data in an array.

Ken

Share this post


Link to post
Share on other sites
Hey... I only got a chance to glance at you guys's code before I got dragged around to places (namely school) but while i was there I managed to come up with something more or less what I was looking for.

Thanks for the help, 'cause even though it's not your code, it still took ideas from it. ^^;

[code]$numelements = 5; //or whatever...
$counter = 0;
$ortest = false;
while($counter != $numelements)
{
if($_POST[{"stuff" . $counter}] != $_POST[{"other" . $counter++}])
{$ortest = true;}
}
if($ortest){echo "Do stuff...";}[/code] is the code in general terms...

[code]//CONNECT
//numcols and numrows already set
$looprow = -1;
while(++$looprow != $numrows)
{
$loopcol = 0;
$ortest = false;
while($loopcol != $numcols)
  {if($_POST[{"r" . $looprow . "c" . $loopcol}] != $_POST[{"oldr" . $looprow . "c" . $loopcol++}])
   {$ortest = true;}
  }
if($ortest){echo "Do update stuff...";}
}

$loopcol = 0;
$ortest = false;
while($loopcol != $numcols)
{if($_POST[{"newc" . $loopcol++}] != ""}])
  {$ortest = true;}
}
if($ortest){echo "Do insert stuff...";}

//other code... post the freaking field names...
mysql_close();[/code]

The top half takes care of the part I was asking you guys about, while the bottom part is basically the same problem with a different application.

With any luck, I'll be able to post the full source code here in... well, before tonite. ...

... PS, my luck sucks. -.-;;

EDIT: Syntax error ><;; Replace every [{ with [ and }] with ] ... -cough- ... Sorry.

Share this post


Link to post
Share on other sites
Finished! ^^ See if you guys can guess what it's supposed to do.

Oh, and proofreading would be appreciated, cause... uh... I don't trust myself. >>; all I know is that it works. ^^

[code]<?php
//MySQL Settings...
$host   = " ";
$user   = " ";
$pass   = " ";
$dbname = " ";
$table  = " ";
?>
<?php
mysql_connect ($host, $user, $pass);
mysql_select_db ($dbname);
if(isset($_POST["r0c0"]))
{
    $numrows = 0;
    $numcols = 0;
    while(isset($_POST["r" . ++$numrows . "c" . $numcols])){;}
    $numrowsb = $numrows - 1;
    while(isset($_POST["r" . $numrowsb . "c" . ++$numcols])){;}
    echo "r$numrows c$numcols <br>";
    $looprow = -1;
    while(++$looprow != $numrows)
    {
        $loopcol = 0;
        $ortest = false;
        while($loopcol != $numcols)
        {
            if($_POST["r" . $looprow . "c" . $loopcol] != $_POST["oldr" . $looprow . "c" . $loopcol++])
            {
                $ortest = true;
            }
        }
        if($ortest)
        {
            $query = "UPDATE $table SET ";
            $loopcol = -1;
            while(++$loopcol != $numcols)
            {
                $query = $query . "`" . $_POST["c" . $loopcol] . "` = '"
                . html_entity_decode($_POST["r" . $looprow . "c" . $loopcol]) . "'";
                if($loopcol != $numcols - 1)
                {
                    $query = $query . ", ";
                }
                else
                {
                    $query = $query . " WHERE ";
                }
            }
            $loopcol = -1;
            while(++$loopcol != $numcols)
            {
                $query = $query . "`" . $_POST["c" . $loopcol] . "` = '"
                . html_entity_decode($_POST["oldr" . $looprow . "c" . $loopcol]) . "'";
                if($loopcol != $numcols - 1)
                {
                    $query = $query . " AND ";
                }
                else
                {
                    $query = $query . " LIMIT 1;";
                }
            }
            mysql_query($query);
        }
    }
    $loopcol = 0;
    $ortest = false;
    while($loopcol != $numcols)
    {
        if($_POST["newc" . $loopcol++] != "")
        {
            $ortest = true;
        }
    }
    if($ortest)
    {
        $query = "INSERT INTO $table VALUES ('";
        $loopcol = -1;
        while(++$loopcol != $numcols)
        {
            $query = $query . html_entity_decode($_POST["newc" . $loopcol]);
            if($loopcol != $numcols - 1)
            {
                $query = $query . "','";
            }
            else
            {
                $query = $query . "');";
            }
        }
        mysql_query($query);
    }
    echo "Database Updated...<br>\n";
}
?>Input Data:<br>
<form action="./dbedit.php" method="post"><table border=1>
<tr><?php
$query = "SELECT * FROM $table";
$data = mysql_query($query);
mysql_close();
$numcols = mysql_num_fields($data);
$numrows = mysql_num_rows($data);
$loopcol = -1;
while(++$loopcol != $numcols)
{
    echo "<td>\""
    . mysql_field_name($data, $loopcol)
    . "\" "
    . mysql_field_type($data, $loopcol)
    . ' ('
    . mysql_field_len($data, $loopcol)
    . ')<br>'
    . mysql_field_flags($data, $loopcol)
    . "</td>\n<input type=\"hidden\" name=\"c"
    . $loopcol . "\" value=\""
    . mysql_field_name($data, $loopcol)
    . "\">\n";
}
echo "</tr>\n\n";
$looprow = -1;
while(++$looprow != $numrows)
{
    echo "<tr>\n";
    $loopcol = -1;
    while(++$loopcol != $numcols)
    {
        echo "<td><input name=\"r{$looprow}c{$loopcol}\" maxlength="
        . mysql_field_len($data, $loopcol)
        . " value=\""
        . htmlentities(mysql_result($data, $looprow, mysql_field_name($data, $loopcol)))
        . "\"></td>\n"
        . "<input type=\"hidden\" name=\"oldr{$looprow}c{$loopcol}\" value=\""
        . htmlentities(mysql_result($data, $looprow, mysql_field_name($data, $loopcol)))
        . "\">\n";
    }
    echo "</tr>\n\n";
}

echo"<tr><td colspan={$numcols}><hr>New Data:</td></tr>\n<tr>\n";
$loopcol = -1;
while(++$loopcol != $numcols)
{
    echo "<td><input name=\"newc{$loopcol}\" maxlength="
    . mysql_field_len($data, $loopcol)
    . "></td>\n";
}
?></tr>
</table><input type="submit" value="Update Database"></form>[/code]

Share this post


Link to post
Share on other sites

×

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.