Jump to content

Table form input to output to 2nd page table


Jayfromsandiego

Recommended Posts

Hello everyone.

I need help with the following PHP APP. I am running on (Version PHP 7.2.10) I am trying to have a page table form on table.php pass the input variable of “5-Numbers” to another page called table_results.php

I want that variable string of “5-Numbers” to be compared against 4 arrays and output any duplicates found within each of those 4 lists. If nothing is found, I still want some visual output that reads “None found”.

 

Lets pretend I have the following example ..

On table.php, I typed inside my table form the 5-Numbers ..

INPUT:  2,15,37,13,28

On table_results.php, the 4 arrays to be compared against my input numbers2,15,37,13,28are ..

 

$array_A = array(2,6,8,11,14,18,24); 

$array_B = array(1,2,9,10,13,14,25,28,);  

$array_C = array(1,3,7,9,13,15,20,21,24); 

$array_D = array(4,5,12,22,23,27,28,29); 

 

So my output should read as follows below ..

OUTPUT: 

 

TABLE

COLUMN 1   COLUMN 2

ROW 1 Matches Found Results ..

ROW 2 GROUP A:            2 

ROW 3 GROUP B:             2,13,28

ROW 4 GROUP ?            13,15

ROW 5 GROUP ?            28

ROW 6 5#s Input:              2,15,37,13,28

 

Please let me know if anyone has any suggestions on how to go about it. 

Thanks.

example.jpg

Link to comment
Share on other sites

Hi there benanamen.

Thank you for reading my post. Let's just say I sell marbles in 4 different categories (GROUP A - RED, GROUP B - BLUE, GROUP C-YELLOW and GROUP D-GREEN) and the 5-Numbers input corresponds to each day of a work week (Monday, Tuesday, Wednesday, Thursday and Friday). I want to keep track of how many marbles are being sold per day from each group category. The numbers between the 4 arrays correspond to sub-categories within each array. 

Link to comment
Share on other sites

This is what I have coded so far for ..

table.php

<?php

session_start();

$_SESSION['regName'] = $regValue;
 
?>
<html>
<head>
<title>ACME WIDGETS :: Type of Marbles Sold Match</title>
</head>
<body>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div align="center"><!-- START :: Data Table -->
<table border="1" cellspacing=0 cellpadding=5 bgcolor="#FFFFFF" bordercolor="#000000">
<th colspan="2" bgcolor="#000000"><span style="font-family: verdana,arial,helvetica; font-size: 15px; font-weight: bold; color: #FFFFFF;">Matches Found Results ..</span></th>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP A:</span></td>
<td>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP B:</span></td>
<td>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP C:</span></td>
<td>&nbsp;</td
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP D:</span></td>
<td>&nbsp;</td
</tr>
<tr>
<td bgcolor="#FF0000"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FFFFFF;">5#s Input:</span></td>
<td>&nbsp;</td
</tr>
</table><!-- END :: Data Table -->
</div>
<div>&nbsp;</div>
<div align="center"><!-- START :: Form -->
<span style='font-weight: bold; color: #FF0000;'>Type in 5 Numbers:</span><br>
<form method="get" action="table_results.php">
    <input type="text" name="regName" value="" autofocus=""><br><br>
    <input type="Submit">
</form>
</div><!-- END :: Form -->
</body>
</html>

Link to comment
Share on other sites

Here is a quickie example. (TMTOWTDI)

 

<?php
$array_A = [2, 6, 8, 11, 14, 18, 24];
$array_B = [1, 2, 9, 10, 13, 14, 25, 28];
$array_C = [1, 3, 7, 9, 13, 15, 20, 21, 24];
$array_D = [4, 5, 12, 22, 23, 27, 28, 29];

$input = '2,15,37,13,28';
$parts = explode(',', $input);

foreach ($parts as $part) {

    if (in_array($part, $array_A)) {
        $group_a[] = $part;
    }

    if (in_array($part, $array_B)) {
        $group_b[] = $part;
    }

    if (in_array($part, $array_C)) {
        $group_c[] = $part;
    }

    if (in_array($part, $array_D)) {
        $group_d[] = $part;
    }
}

echo '<pre>', print_r($group_a, true), '</pre>';
echo '<pre>', print_r($group_b, true), '</pre>';
echo '<pre>', print_r($group_c, true), '</pre>';
echo '<pre>', print_r($group_d, true), '</pre>';

 

Link to comment
Share on other sites

Okay.

I tried this code and its only echoing "Array" for Group A, Group B, Group C and Group D.

Wonder why its not working? ?

table_results.php

<?php
       
$input = $_REQUEST['regName'];
                
$array_A = [2, 6, 8, 11, 14, 18, 24];
$array_B = [1, 2, 9, 10, 13, 14, 25, 28];
$array_C = [1, 3, 7, 9, 13, 15, 20, 21, 24];
$array_D = [4, 5, 12, 22, 23, 27, 28, 29];

$parts = explode(",", $input);

foreach ($parts as $part) {

    if (in_array($part, $array_A)) {
        $group_a[] = $part;
    }

    if (in_array($part, $array_B)) {
        $group_b[] = $part;
    }

    if (in_array($part, $array_C)) {
        $group_c[] = $part;
    }

    if (in_array($part, $array_D)) {
        $group_d[] = $part;
    }
}
         
?>
<html>
<head>
<title>ACME WIDGETS :: Type of Marbles Sold Match</title>
</head>
<body>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div align="center">
<table border="1" cellspacing="0" cellpadding="5" bgcolor="#FFFFFF" bordercolor="#000000">
<th colspan="2" bgcolor="#000000"><span style="font-family: verdana,arial,helvetica; font-size: 15px; font-weight: bold; color: #FFFFFF;">Matches Found Results ..</span></th>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP A:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF0000;"><?php echo $group_a; ?></span></td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP B:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF0000;"><?php echo $group_b; ?></span></td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP C:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF0000;"><?php echo $group_c; ?></span></td
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP D:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF8C00;"><?php echo $group_d; ?></span></td
</tr>
<tr>
<td bgcolor="#FF0000"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FFFFFF;">5#s Input:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #00CC00;"><?php echo $input; ?></span></td
</tr>
</table>
</div>
<div>&nbsp;</div>
</body>
</html>

Link to comment
Share on other sites

Alright!! I solved it. What a great way to start the new year in 2019. ? The problem was in the echo command and was changed from <?php echo $group_a; ?>  to  <?php echo join(',',$group_a); ?>

Here's the final working code in case anyone else ever needs something similar in the future (just need an "if else"  implemented when no matches are found).

--------------------- PAGE 1

table.php

<?php

session_start();

$_SESSION['regName'] = $input;
         
?>
<html>
<head>
<title>ACME WIDGETS :: Type of Marbles Sold Match</title>
</head>
<body>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div align="center"><!-- START :: Data Table -->
<table border="1" cellspacing=0 cellpadding=5 bgcolor="#FFFFFF" bordercolor="#000000">
<th colspan="2" bgcolor="#000000"><span style="font-family: verdana,arial,helvetica; font-size: 15px; font-weight: bold; color: #FFFFFF;">Find Matching Results ..</span></th>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP A:</span></td>
<td>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP B:</span></td>
<td>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP C</span></td>
<td>&nbsp;</td
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP D </span></td>
<td>&nbsp;</td
</tr>
<tr>
<td bgcolor="#FF0000"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FFFFFF;">5#s Input:</span></td>
<td>&nbsp;</td
</tr>
</table><!-- END :: Data Table -->
</div>
<div>&nbsp;</div>
<div align="center"><!-- START :: Form -->
<span style='font-weight: bold; color: #FF0000;'>Type in 5 Numbers:</span><br>
<form method="get" action="table_results.php">
    <input type="text" name="regName" value="" autofocus=""><br><br>
    <input type="Submit">
</form>
</div><!-- END :: Form -->
</body>
</html>

--------------------- PAGE 2

table_results.php

<?php
       
$input = $_REQUEST['regName'];
           
$array_A = [2, 6, 8, 11, 14, 18, 24];
$array_B = [1, 2, 9, 10, 13, 14, 25, 28];
$array_C = [1, 3, 7, 9, 13, 15, 20, 21, 24];
$array_D = [4, 5, 12, 22, 23, 27, 28, 29];

$parts = explode(",", $input);

foreach ($parts as $part) {

    if (in_array($part, $array_A)) {
        $group_a[] = $part;
    }

    if (in_array($part, $array_B)) {
        $group_b[] = $part;
    }

    if (in_array($part, $array_C)) {
        $group_c[] = $part;
    }

    if (in_array($part, $array_D)) {
        $group_d[] = $part;
    }
}
         
?>
<html>
<head>
<title>ACME WIDGETS :: Type of Marbles Sold Match</title>
</head>
<body>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div align="center">
<table border="1" cellspacing="0" cellpadding="5" bgcolor="#FFFFFF" bordercolor="#000000">
<th colspan="2" bgcolor="#000000"><span style="font-family: verdana,arial,helvetica; font-size: 15px; font-weight: bold; color: #FFFFFF;">Matches Found Results ..</span></th>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP A:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF0000;"><?php echo join(',',$group_a); ?></span></td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP B:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF0000;"><?php echo join(',',$group_b); ?></span></td>
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP C</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF0000;"><?php echo join(',',$group_c); ?></span></td
</tr>
<tr>
<td bgcolor="#DCDCDC"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #000000;">GROUP D</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FF8C00;"><?php echo join(',',$group_d); ?></span></td
</tr>
<tr>
<td bgcolor="#FF0000"><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #FFFFFF;">5#s Input:</span></td>
<td><span style="font-family: verdana,arial,helvetica; font-size: 12px; font-weight: bold; color: #00CC00;"><?php echo $input; ?></span></td
</tr>
</table>
</div>
<div>&nbsp;</div>
</body>
</html>

Link to comment
Share on other sites

Use array_intersect(), arrays, and CSS and save yourself all that repetition

<?php
$arrays = [
            'A' => [2, 6, 8, 11, 14, 18, 24],
            'B' => [1, 2, 9, 10, 13, 14, 25, 28],
            'C' => [1, 3, 7, 9, 13, 15, 20, 21, 24],
            'D' => [4, 5, 12, 22, 23, 27, 28, 29]
          ];
$input = '2,15,37,13,28';
$parts = explode(',', $input);
?>
<!DOCTYPE html>
<html>
<title>Example</title>
<style type='text/css'>
    table {
        border-collapse: collapse;
    }
    th, td {
        font-family: verdana,arial,helvetica,sans-serif; 
        font-size: 12px; 
    }
    th {
        background-color: #DCDCDC;
        color: #000;
        text-align: left;
        padding: 4px;
    }
    td {
        padding: 4px;
    }
</style>
<body>
     <table border='1'>
         <?php
            foreach ($arrays as $gp => $a) {
                echo "<tr><th>GROUP $gp</th><td>" . join(', ', array_intersect($a, $parts) ) . "</td></tr>\n";
            }
         ?>
     </table>
</body>
</html>

 

Capture.PNG

Link to comment
Share on other sites

Archived

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

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