Jump to content


Photo

Infinite OR's?


  • Please log in to reply
6 replies to this topic

#1 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 April 2006 - 01:03 PM

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:
if($a == $a2 || $b == $b2 || $c == $c2 [...])

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

Hm... any ideas?

Thanks for reading. ^^

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 17 April 2006 - 01:30 PM

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?


#3 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 April 2006 - 01:39 PM

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
if($a == $a2 || $b == $b2)

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)
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...";}

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...

#4 Darkness Soul

Darkness Soul
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts
  • LocationBrazil; São Paulo.

Posted 17 April 2006 - 03:08 PM

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

<?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! =)';
    }
?>

(If something is wrong, please tell me. I'm learning this language. Thank you)

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 April 2006 - 04:47 PM

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:
<?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
//
    }
}
?>

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

#6 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 18 April 2006 - 10:14 PM

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. ^^;

$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...";}
is the code in general terms...

//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();

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.

#7 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 19 April 2006 - 02:06 AM

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. ^^

<?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>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users