We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10300
    • 7 Posts
    I have the following code for a page I need to include in my MODX CMS. I find it difficult reconcilling it in my MODX how can I do it. Please help

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <?php
    echo "<table width=’100%’ border=’1’><tr> <td>
    <form id=’form1’ name=’form1’ method=’post’ action=’testApplication.php’>
    <table width=’100%’ border=’0’>
    <tr>
    <td>Search Profile </td>
    <td><input name=’q’ type=’text’ id=’q’ /></td>
    <td><select name=’s’ >
    <option value=’first_name’ selected=’selected’>First Name</option>
    <option value=’Surname’>Surname</option>
    <option value=’Designation’>Designation</option>
    <option value=’Office_extension ’>Extension</option>
    <option value=’Official_email’>Email</option>
    </select></td>
    <td><input type=’submit’ name=’Submit’ value=’Search’ /></td>
    </tr>
    </table>
    </form>
    </td>
    </tr><tr><td>";


    $var = $_POST[’q’] ;
    $var2 = $_POST[’s’] ;
    $trimmed = trim($var); //trim whitespace from the stored variable

    // check for an empty string and display a message.
    if ($trimmed == "")
    {
    echo "<p>Please enter a search...</p>";
    exit();
    }

    // Connects to your Database
    mysql_connect("localhost", "root", "Sysoper234") or die(mysql_error());
    mysql_select_db("nnradb") or die(mysql_error());


    $variable="’".$trimmed."%’";
    $sql = "select * from tb_staff_record where ".$var2." like ".$variable;

    $data = mysql_query($sql) or die(mysql_error());
    $rows = mysql_num_rows($data);

    if ($rows==0)
    {
    echo "<h4>Results</h4>";
    echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
    exit();
    }
    else
    {

    Print "<table width=’100%’ border=’0’>
    <tr>
    <td >S/No </td>
    <td >File No </td>
    <td >Surname</td>
    <td >First Name </td>
    <td >Middle Name </td>
    <td >Designation</td>
    <td >Email</td>
    </tr>";

    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }

    //Here we count the number of results
    //Edit $data to be your query
    $data = mysql_query($sql) or die(mysql_error());
    $rows = mysql_num_rows($data);

    //This is the number of results displayed per page
    $page_rows = 30;

    //This tells us the page number of our last page
    $last = ceil($rows/$page_rows);

    //this makes sure the page number isn’t below one, or more than our maximum pages
    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    }

    $order="ORDER BY ".$var2." ASC ";
    //This sets range that we will display in our query
    $max = ’limit ’ .($pagenum - 1) * $page_rows .’,’ .$page_rows;

    //This is your query again, the same one... the only difference is we add $max into it
    $data_p = mysql_query($sql.$order.$max) or die(mysql_error());

    //This is where you display your query results
    $count = 1 + $s ;
    while($info = mysql_fetch_array( $data_p ))
    {
    Print "<tr>";
    Print "<td >".$count."</td>";
    Print "<td>".$info[’File_no’]."</td>";
    Print "<td>".$info[’Surname’]."</td>";
    Print "<td>".$info[’First_name’]."</td>";
    Print "<td>".$info[’Middle_name’]."</td>";
    Print "<td>".$info[’Designation’]."</td>";
    Print "<td>".$info[’Official_email’]."</td>";
    Print "</tr>";


    //Print $info[’Surname’];
    $count++;
    }
    Print "<tr><td colspan=’7’>";

    // This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";

    // First we check if we are on page one. If we are then we don’t need a link to the previous page or the first page so we do nothing. If we aren’t then we generate links to the first page, and to the previous page.
    if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href=’{$_SERVER[’testApplication.php’]}?pagenum=1’> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href=’{$_SERVER[’testApplication.php’]}?pagenum=$previous’> <-Previous</a> ";
    }
    //echo $PHP_SELF;


    //just a spacer
    //echo " ---- ";

    //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href=’{$_SERVER[’query.php’]}?pagenum=$next’>Next -></a> ";
    echo " ";
    echo " <a href=’{$_SERVER[’query.php’]}?pagenum=$last’>Last ->></a> ";
    }
    echo "</tr></td></table>";
    echo "</table>";
    }

    Print "</td></tr></table>";

    ?>
    </body>
    </html>
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      Please modify your title, so others can find it easier if needed.

      Something like "Porting php to modx"-ish would work?


      For Revolution...
      - You can put any html straight into a Template (Elements -> Templates, right click to make a new one). Then, you can modify pieces of text with any page-specific information using the [[*field]] tag. For example your title:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>[[*pagetitle]]</title>
      </head>
      <body>
      <?php
         <table width='100%' border='1'><tr> <td>
            <form id='form1' name='form1' method='post' action='testApplication.php'>
              <table width='100%' border='0'>
                <tr>
                  <td>Search Profile </td>
                  <td><input name='q' type='text' id='q' /></td>
                  <td>
                    <select name='s' >
                    <option value='first_name' selected='selected'>First Name</option>
                    <option value='Surname'>Surname</option>
                    <option value='Designation'>Designation</option>
                    <option value='Office_extension'>Extension</option>
                    <option value='Official_email'>Email</option>
                    </select>
                  </td>
                  <td><input type='submit' name='Submit' value='Search' /></td>
                </tr>
              </table>
            </form>
         </td>
        </tr><tr><td>";



      Any HTML that should appear on any page using the template should be added to the template.


      Moving over your application will be slightly more difficult. To start with, you’ll want to put it all in a snippet and call it (uncached, as it responds to user-posted data). If you name your snippet testApplication, you can call it with [[!testApplication]]. If I were you, I’d put this snippet on one page, and make sure the form posts to that page, by using [[~7]] as the action link (if 7 was the ID of the resource the snippet is on).

      As snippets behave like php functions so instead of echoing, collect all data in an $ouput variable ($output .= ’blah’) and use the return statement at the end of the snippet.

      Where you’re using for example ’{$_SERVER[’testApplication.php’]}?pagenum=1 you can use something like this: $modx->makeUrl(’7’,’’,array(’pagenum’ => 1)).


      Hope this helps.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 10300
        • 7 Posts
        Quote from: Mark at Jan 06, 2011, 10:16 AM


        Where you’re using for example ’{$_SERVER[’testApplication.php’]}?pagenum=1 you can use something like this: $modx->makeUrl(’7’,’’,array(’pagenum’ => 1)).


        Hope this helps.

        Thank you for you prompt response. can you pls explain the properties of makeUrl u mention above so I can be sure of what I’m doing.
          • 4172
          • 5,888 Posts
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 10300
            • 7 Posts
            <?php
            $output = "<table width=’100%’ border=’1’><tr> <td> <form method=post action=[[~5]]>";
            $output.= " <table width=100% border=0> <tr> <td>Search Profile </td> <td><input name=q type=text id=q /></td><td><select name=s >";
            $output.= "<option value=first_name selected=selected>First Name</option> <option value=Surname>Surname</option>";
            $output.= "<option value=Designation>Designation</option><option value=Office_extension >Extension</option>";
            $output.= "<option value=Official_email>Email</option></select></td> <td><input type=submit name=Submit value=Search /></td>";
            $output.= "</tr> </table> </form></td> </tr><tr><td>";


            $var = $_POST[’q’] ;
            $var2 = $_POST[’s’] ;
            $trimmed = trim($var); //trim whitespace from the stored variable

            // check for an empty string and display a message.
            if ($trimmed == "")
            {
            $output.= "<p>Please enter a search...</p>";
            exit();
            }

            // Connects to your Database
            mysql_connect("localhost", "root", "Sysoper234") or die(mysql_error());
            mysql_select_db("nnradb") or die(mysql_error());


            $variable="’".$trimmed."%’";
            $sql = "select * from tb_staff_record where ".$var2." like ".$variable;

            $data = mysql_query($sql) or die(mysql_error());
            $rows = mysql_num_rows($data);

            if ($rows==0)
            {
            $output.= "<h4>Results</h4>";
            $output.= "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
            exit();
            }
            else
            {

            $output.= "<table width=’100%’ border=’0’>
            <tr>
            <td >S/No </td>
            <td >File No </td>
            <td >Surname</td>
            <td >First Name </td>
            <td >Middle Name </td>
            <td >Designation</td>
            <td >Email</td>
            </tr>";

            //This checks to see if there is a page number. If not, it will set it to page 1
            if (!(isset($pagenum)))
            {
            $pagenum = 1;
            }

            //Here we count the number of results
            //Edit $data to be your query
            $data = mysql_query($sql) or die(mysql_error());
            $rows = mysql_num_rows($data);

            //This is the number of results displayed per page
            $page_rows = 30;

            //This tells us the page number of our last page
            $last = ceil($rows/$page_rows);

            //this makes sure the page number isnt below one, or more than our maximum pages
            if ($pagenum < 1)
            {
            $pagenum = 1;
            }
            elseif ($pagenum > $last)
            {
            $pagenum = $last;
            }

            $order="ORDER BY ".$var2." ASC ";
            //This sets range that we will display in our query
            $max = ’limit ’ .($pagenum - 1) * $page_rows .’,’ .$page_rows;

            //This is your query again, the same one... the only difference is we add $max into it
            $data_p = mysql_query($sql.$order.$max) or die(mysql_error());

            //This is where you display your query results
            $count = 1 + $s ;
            while($info = mysql_fetch_array( $data_p ))
            {
            $output.= "<tr>";
            $output.= "<td >".$count."</td>";
            $output.= "<td>".$info[’File_no’]."</td>";
            $output.= "<td>".$info[’Surname’]."</td>";
            $output.= "<td>".$info[’First_name’]."</td>";
            $output.= "<td>".$info[’Middle_name’]."</td>";
            $output.= "<td>".$info[’Designation’]."</td>";
            $output.= "<td>".$info[’Official_email’]."</td>";
            $output.= "</tr>";


            //Print $info[’Surname’];
            $count++;
            }
            Print "<tr><td colspan=’7’>";

            // This shows the user what page they are on, and the total number of pages
            $output.= " --Page $pagenum of $last-- <p>";

            // First we check if we are on page one. If we are then we don’t need a link to the previous page or the first page so we do nothing. If we aren’t then we generate links to the first page, and to the previous page.
            if ($pagenum == 1)
            {
            }
            else
            {
            $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = 1))’> <<-First</a> ";
            $output.= " ";
            $previous = $pagenum-1;
            $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = $previous))’> <-Previous</a> ";
            }
            $PHP_SELF;


            //just a spacer
            //$output.= " ---- ";

            //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
            if ($pagenum == $last)
            {
            }
            else {
            $next = $pagenum+1;
            $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = $next))’>Next -></a> ";
            $output.= " ";
            $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = $last))’>Last ->></a> ";
            }
            $output.= "</tr></td></table>";
            $output.= "</table>";
            }
            return $output;
            ?>

            I have type the above code for my snippet but its not displaying anything. The idea of the code is to enable be display results dynamically based on search criterion. Pls help
            Thanks
              • 4172
              • 5,888 Posts
              $var = $_POST['q'] ;
                $var2 = $_POST['s'] ;
                $trimmed = trim($var); //trim whitespace from the stored variable
              
              // check for an empty string and display a message.
              if ($trimmed == "")
                {
                $output.=   "<p>Please enter a search...</p>";
                exit();
                }


              this will exit() if nothing is posted.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 10300
                • 7 Posts
                Counting on you help please
                  • 10300
                  • 7 Posts
                  Thanks bruno
                  The form now displays on the page. but I still have a problem with it.
                  when I enter my search criterion and submit the form it takes me to the index page instead of displaying data from the database the index of the page that I ported the Snippet in is 5 (same page) and the below is the value of the form submission.

                  <form method=post action=[[~5]]>";
                    • 18373 ☆ A M B ☆
                    • 3,141 Posts
                    Just to confirm: you are using MODx Revolution right?

                    If you’re on Evolution you will need to change [[~5]] to [~5~].

                    As general "best practice" be sure to enclose your attributes in quotes (so method="post" instead of method=post), also for the action attribute. Maybe (if you’re using friendly urls for example) the not-using-quotes breaks it due to a slash or anything (just thinking out loud now).
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 10300
                      • 7 Posts
                      <?php
                      $output = "<table width=’100%’ border=’1’><tr> <td> <form method=post action=[[~5~]]>";
                      $output.= " <table width=100% border=0> <tr> <td>Search Profile </td> <td><input name=q type=text id=q /></td><td><select name=s >";
                      $output.= "<option value=first_name selected=selected>First Name</option> <option value=Surname>Surname</option>";
                      $output.= "<option value=Designation>Designation</option><option value=Office_extension >Extension</option>";
                      $output.= "<option value=Official_email>Email</option></select></td> <td><input type=submit name=Submit value=Search /></td>";
                      $output.= "</tr> </table> </form></td> </tr><tr><td>";


                      $var = $_POST[’q’] ;
                      $var2 = $_POST[’s’] ;
                      $trimmed = trim($var); //trim whitespace from the stored variable

                      // check for an empty string and display a message.
                      if ($trimmed == "")
                      {
                      $output.= "<p>Please enter a search...</p>";
                      }
                      else{
                      // Connects to your Database
                      mysql_connect("localhost", "root", "Sysoper234") or die(mysql_error());
                      mysql_select_db("nnradb") or die(mysql_error());


                      $variable="’".$trimmed."%’";
                      $sql = "select * from tb_staff_record where ".$var2." like ".$variable;

                      $data = mysql_query($sql) or die(mysql_error());
                      $rows = mysql_num_rows($data);

                      if ($rows==0)
                      {
                      $output.= "<h4>Results</h4>";
                      $output.= "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
                      exit();
                      }
                      else
                      {

                      $output.= "<table width=’100%’ border=’0’>
                      <tr>
                      <td >S/No </td>
                      <td >File No </td>
                      <td >Surname</td>
                      <td >First Name </td>
                      <td >Middle Name </td>
                      <td >Designation</td>
                      <td >Email</td>
                      </tr>";

                      //This checks to see if there is a page number. If not, it will set it to page 1
                      if (!(isset($pagenum)))
                      {
                      $pagenum = 1;
                      }

                      //Here we count the number of results
                      //Edit $data to be your query
                      $data = mysql_query($sql) or die(mysql_error());
                      $rows = mysql_num_rows($data);

                      //This is the number of results displayed per page
                      $page_rows = 30;

                      //This tells us the page number of our last page
                      $last = ceil($rows/$page_rows);

                      //this makes sure the page number isnt below one, or more than our maximum pages
                      if ($pagenum < 1)
                      {
                      $pagenum = 1;
                      }
                      elseif ($pagenum > $last)
                      {
                      $pagenum = $last;
                      }

                      $order="ORDER BY ".$var2." ASC ";
                      //This sets range that we will display in our query
                      $max = ’limit ’ .($pagenum - 1) * $page_rows .’,’ .$page_rows;

                      //This is your query again, the same one... the only difference is we add $max into it
                      $data_p = mysql_query($sql.$order.$max) or die(mysql_error());

                      //This is where you display your query results
                      $count = 1 + $s ;
                      while($info = mysql_fetch_array( $data_p ))
                      {
                      $output.= "<tr>";
                      $output.= "<td >".$count."</td>";
                      $output.= "<td>".$info[’File_no’]."</td>";
                      $output.= "<td>".$info[’Surname’]."</td>";
                      $output.= "<td>".$info[’First_name’]."</td>";
                      $output.= "<td>".$info[’Middle_name’]."</td>";
                      $output.= "<td>".$info[’Designation’]."</td>";
                      $output.= "<td>".$info[’Official_email’]."</td>";
                      $output.= "</tr>";


                      //Print $info[’Surname’];
                      $count++;
                      }
                      Print "<tr><td colspan=’7’>";

                      // This shows the user what page they are on, and the total number of pages
                      $output.= " --Page $pagenum of $last-- <p>";

                      // First we check if we are on page one. If we are then we don’t need a link to the previous page or the first page so we do nothing. If we aren’t then we generate links to the first page, and to the previous page.
                      if ($pagenum == 1)
                      {
                      }
                      else
                      {
                      $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = 1))’> <<-First</a> ";
                      $output.= " ";
                      $previous = $pagenum-1;
                      $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = $previous))’> <-Previous</a> ";
                      }
                      $PHP_SELF;


                      //just a spacer
                      //$output.= " ---- ";

                      //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
                      if ($pagenum == $last)
                      {
                      }
                      else {
                      $next = $pagenum+1;
                      $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = $next))’>Next -></a> ";
                      $output.= " ";
                      $output.= " <a href=’$modx->makeUrl(’5’,’’,array(’pagenum’ = $last))’>Last ->></a> ";
                      }
                      $output.= "</tr></td></table>";
                      $output.= "</table>";
                      }
                      }
                      return $output;
                      ?>

                      I have tried everything I think I know but its not working I will appreciate if you can help go thru this code and make the corrections

                      THank you very much in anticipation

                      I Use MODX Version 0.9.6.3