简体   繁体   中英

Retrieve same data from same table twice

I have a form with 6 different fields to enter dates ( day1 to day6 ) and 6 different fields enter details of that particular dates( dtls1 to dtls6 ). These all data will go to a database. And there is a page to display all these data. The selection is according to date. Code is given below....

$today = date('D, d M, Y');
$sql = "SELECT * FROM tables WHERE day1 = '$today' AND org = 'green'";
if($_POST!=""){
 $mydate = mysql_real_escape_string($_POST['datepicker']);
    if($mydate!=""){    
        $sql = "SELECT * FROM tables WHERE day1 = '$mydate' AND org = 'green'"; 
    }       
}
$string = nl2br($string);
$num_results_per_page = 2;
$num_page_links_per_page = 5;
$pg_param1 = ""; // Ex: &q=value
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
pagination($sql, $num_results_per_page, $num_page_links_per_page, $pg_param);
if($pg_error == '')
{
    if(mysql_num_rows($pg_result) > 0)
    {
        while($data = mysql_fetch_assoc($pg_result))
        {

echo "<table width=585 height=203>
        <tr>
            <td height=10 colspan=3 align=left valign=top></td>
            </tr>
          <tr>
            <td height=17 colspan=3 align=center valign=middle class=text6>". $data['pgmtitle'] .".</td>
            </tr>
          <tr>
            <td height=17 colspan=3 align=center valign=middle class=text8>". $data['org'] .".</td>
            </tr>
          <tr>
            <td height=21 colspan=3 align=center valign=middle class=text19>". $data['programdtls1'] ." OR</td>
            </tr>
  <tr>
            <td width=190 height=32 align=left valign=top class=text3>". $data['venue'] ."</td>
            <td width=11 rowspan=2 align=left valign=top><div class=box2></div></td>
            <td width=368 rowspan=2 align=left valign=top class=text7>
                    <b>Contact:</b> ". $data['contactperson'] ."
                    <br/><b>Phone:</b> ". $data['contactnumber'] ."
                    <br/><b>E-mail ID:</b> ". $data['email'] ."
                    <br/><b>Website:</b> ". $data['website'] ."</td>
          </tr></table><br/>"

echo "</br>". $pagination_output; 
    }
    else
    {
        echo '<p class="text11">No events listed for this day.</p>';
    }
}
else
{
    echo $pg_error; //display any errors, you can remove this if you want.  
}
?>

This code is working fine. the problem is I want to display if anything is there with (programdtls2 to programdtls6). The thing is that with programdtls2 I want to show venue, contact person, contact number etc. as same as programdtls1. Like...

echo "<table width=585 height=203>
<tr>
<td height=10 colspan=3 align=left valign=top></td>
 </tr>
 <tr>
 <td height=17 colspan=3 align=center valign=middle class=text6>". $data['pgmtitle'] .".</td>
  </tr>
  <tr>
   <td height=17 colspan=3 align=center valign=middle class=text8>". $data['org'] .".</td>
   </tr>
   <tr>
   <td height=21 colspan=3 align=center valign=middle class=text19>". $data['programdtls2'] ."</td>
   </tr>
   <tr>
   <td width=190 height=32 align=left valign=top class=text3>". $data['venue']     ."</td>
   <td width=11 rowspan=2 align=left valign=top><div class=box2></div></td>
   <td width=368 rowspan=2 align=left valign=top class=text7>
                    <b>Contact:</b> ". $data['contactperson'] ."
                    <br/><b>Phone:</b> ". $data['contactnumber'] ."
                    <br/><b>E-mail ID:</b> ". $data['email'] ."
                    <br/><b>Website:</b> ". $data['website'] ."</td>
    </tr></table><br/>"

table structure

"id_user INT NOT NULL AUTO_INCREMENT ,".
                "org VARCHAR( 100 ) NOT NULL ,".
                "pgmtitle VARCHAR( 100 ) NOT NULL ,".
                "category VARCHAR( 32 ) NOT NULL ,".
                "relation VARCHAR( 75 ) NOT NULL ,".
                "venue VARCHAR( 75 ) NOT NULL ,".
                "hr VARCHAR( 30 ) NOT NULL ,".
                "min VARCHAR( 30 ) NOT NULL ,".
                "time VARCHAR( 30 ) NOT NULL ,".
                "day1 VARCHAR( 30 ) NOT NULL ,".
                "day2 VARCHAR( 30 ) NOT NULL ,".
                "day3 VARCHAR( 30 ) NOT NULL ,".
                "day4 VARCHAR( 30 ) NOT NULL ,".
                "day5 VARCHAR( 30 ) NOT NULL ,".
                "day6 VARCHAR( 30 ) NOT NULL ,".
                "city VARCHAR( 35 ) NOT NULL ,".
                "others VARCHAR( 35 ) NOT NULL ,".
                "contactperson VARCHAR( 128 ) NOT NULL ,".
                "contactnumber VARCHAR( 128 ) NOT NULL ,".
                "email VARCHAR( 64 ) NOT NULL ,".
                "programdtls1 VARCHAR( 150 ) NOT NULL ,".
                "programdtls2 VARCHAR( 150 ) NOT NULL ,".
                "programdtls3 VARCHAR( 150 ) NOT NULL ,".
                "programdtls4 VARCHAR( 150 ) NOT NULL ,".
                "programdtls5 VARCHAR( 150 ) NOT NULL ,".
                "programdtls6 VARCHAR( 150 ) NOT NULL ,".
                "nature VARCHAR( 50 ) NOT NULL ,".
                "resentry VARCHAR( 50 ) NOT NULL ,".
                "confirmcode VARCHAR(32) ,".
                "PRIMARY KEY ( id_user )".
                ")";

Is this possible

If I'm reading this correctly you want programdtls2 to superseed programdtls1 if it is set, which can be done using a simple if-else-statement.

if($data['programdtls2'] != '')
{
  // programdtls2 is NOT empty, so superseed programdtls1:
  echo $data['programdtls2'];
}
else
{
  echo $data['programdtls1'];
}

The code above is just a simple example of what I think you want and should be doing. You can also do this check inline, like this:

(($data['programdtls2'] != '') ? $data['programdtls2'] : $data['programdtls1'])

This means if not empty do dtls2 else do dtls1 If you combine it with the code you already have, you get this. Note that this code is copied from your original and thus contains the same mistakes, like missing semi-colons ( ; ).

echo "<table width=585 height=203>
        <tr>
          <td height=10 colspan=3 align=left valign=top></td>
        </tr>
        <tr>
          <td height=17 colspan=3 align=center valign=middle class=text6>
            ". $data['pgmtitle'] .".
          </td>
        </tr>
        <tr>
          <td height=17 colspan=3 align=center valign=middle class=text8>
            ". $data['org'] .".
          </td>
        </tr>
        <tr>
          <td height=21 colspan=3 align=center valign=middle class=text19>
            ". (($data['programdtls2'] != '') ? 
              $data['programdtls2'] : $data['programdtls1']) ." OR
          </td>
        </tr>
        <tr>
          <td width=190 height=32 align=left valign=top class=text3>
            ". $data['venue'] ."
          </td>
          <td width=11 rowspan=2 align=left valign=top><div class=box2></div></td>
          <td width=368 rowspan=2 align=left valign=top class=text7>
            <b>Contact:</b> ". $data['contactperson'] ."<br/>
            <b>Phone:</b> ". $data['contactnumber'] ."<br/>
            <b>E-mail ID:</b> ". $data['email'] ."<br/>
            <b>Website:</b> ". $data['website'] ."</td>
        </tr>
    </table>
<br/>";
echo "</br>". $pagination_output;

Also, I will not go into database design too much here, but on first sight the design you have is sub-optimal. Simple example, you reserve 6 days and 6 events for all rows in the database, but some only have 1 while others will have the full 6. Better to split things up in multiple tables, like:

  1. Data (Event Name, Organiser, Contact Information, etc.)
  2. Days (Event Days)
  3. Content (Event Content)

You can combine the days and content if there is only one content each day. Furthermore if there is one organiser for multiple events you might consider an extra table for those (table: Organiser, which contains the contact information). This way you keep your database size to a minimum and also make less mistakes.

One last sidenote, please check your HTML markup too. Your markup will force all browsers to go into Quirks-Mode, which is not consistent accross PC's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM