www.webdeveloper.com
Results 1 to 9 of 9

Thread: data in one table

  1. #1
    Join Date
    Feb 2017
    Posts
    43

    data in one table

    Hi,

    I have a function that creates table but when I fed all the data to the function it gives me for each data new table and then it displays more tables.
    For example, if I have 3 data such as name, ISBN, price etc. It will display each name and so on in one table so I end up with 3 tables.

    The reason I do believe is that I'm calling the function in while statement and concatenate each row wit mysqli_fetch_assoc

    here is the code

    PHP Code:
    //select all books
    $sql "SELECT title, isbn, published, price FROM book";
    $result mysqli_query($link$sql);

    //check query
    if($result === false){
        
    $content .= mysqli_error($link);
    } else {
        
    //fetch associative array with data 
        
    $con '';
        while (
    $row mysqli_fetch_assoc($result)){
             
    $con .= table($row['title'],$row['isbn'],$row['published'],$row['price'],'NULL') . '</br>';
        }
        
    // free result set
        
    mysqli_free_result($result);
    }

    $page_name 'Books';
    $content .= "<h1>Welcome to the $page_name page</h1>";

    $name 'Prioritizing Web Usability';
    $isbn '978-0321350312'
    $date date"Y-m-d"strtotime"2009-01-31 +1 month" ));
    $cost formatMoney(24.99);

    $a2 = array('Author 1''Author 2');
    $author list_authors($a2);

    $content .= table($name,$isbn,$date,$cost$author) . '</br>';

    // this is the calling out to display as HTML on the browser
    $content .= $con
    the function for table look like this
    PHP Code:
    function table($title,$number,$date,$price,$author){
        return 
    '<table style="width:60%">
      <tr>
        <th>Title</th>
        <th>ISBN</th> 
        <th>Published</th>
        <th>Price</th>
        <th>Author(s)</th>
      </tr>
      <tr>
        <td>' 
    htmlentities($title) . '</td>
        <td>' 
    htmlentities($number) . '</td>
        <td>' 
    htmlentities($date) . '</td>
        <td>'
    '' htmlentities($price) . '</td>
        <td>' 
    strip_tags($author) . '</td>
      </tr>
    </table>'
    ;


    please ignore the 1st content $content .= table($name,$isbn,$date,$cost, $author) . '</br>'; that is something from previous exercise.



    thanks for any ideas

    Your newbie

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,327
    If you're going to call a table() function from within your while() loop, then it should only output a row element, not the entire table. Maybe the simplest thing would be to fetch all the DB results into an array with the mysqli_fetch_all() method, then feed that array into your function. At that point, the function would then do a loop on that array to output the table rows.
    ___
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Feb 2017
    Posts
    43
    Quote Originally Posted by NogDog View Post
    If you're going to call a table() function from within your while() loop, then it should only output a row element, not the entire table. Maybe the simplest thing would be to fetch all the DB results into an array with the mysqli_fetch_all() method, then feed that array into your function. At that point, the function would then do a loop on that array to output the table rows.
    ___


    I have tried to follow you but it give me only one row

    PHP Code:
        while ($row mysqli_fetch_assoc($result)){
             
    /* $con .= table($row['title'],$row['isbn'],$row['published'],$row['price'],'NULL') . '</br>'; */
             
             
    $new_data = array($row);
             
    $display table($new_data[0]['title'], 
                        
    $new_data[0]['isbn'],
                        
    $new_data[0]['published'],
                        
    $new_data[0]['price'], 'NULL');
             
        } 

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,327
    I'm thinking more along these lines, perhaps, which would generalize the table() function for use with a variety of MySQLi queries.
    PHP Code:
    function table(mysqli_result $result, Array $columns)
    {
      
    $output '<table style="width:60%">
      <tr>
        <th>Title</th>
        <th>ISBN</th> 
        <th>Published</th>
        <th>Price</th>
        <th>Author(s)</th>
      </tr>
    '
    ;
      while((
    $row mysqli_fetch_assoc($result)) != false) {
        
    $output .= '  <tr>';
        foreach(
    $columns as $col) {
          
    $output .= '    <td>'.htmlentities($row[$col]).'</td>';
        }
        
    $output .= "</tr>\n";
      }
      
    $output .= "</table>\n";
      return 
    $output;
    }

    # usage:

    $result mysqli_query($link$sql);
    echo 
    table($result, array('title''isbn''published''price')) 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,327
    Revised to make the column heading defined in the 2nd param, as well:
    PHP Code:
    function table(mysqli_result $result, Array $columns)
    {
      
    $output '<table style="width:60%">
      <tr>'
    ;
        foreach(
    $columns as $title => $col) {
          
    $output .= '<th>'.htmlentities($title).'</th>';
        }
      
    $output .= "</tr>\n";
      while((
    $row mysqli_fetch_assoc($result)) != false) {
        
    $output .= '<tr>';
        foreach(
    $columns as $col) {
          
    $output .= '    <td>'.htmlentities($row[$col]).'</td>';
        }
        
    $output .= "  </tr>\n";
      }
      
    $output .= "</table>\n";
      return 
    $output;
    }

    # usage:

    $result mysqli_query($link$sql);
    echo 
    table($result, array('Title' => 'title''ISBN' => 'isbn''Published' => 'published''Price' => 'price')); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Join Date
    Feb 2017
    Posts
    43
    ohh I see you rewrite the function table(). Which I was thinking to do it as well but I was trying to use already what I had but I see that sometimes we just have to change some code to get it work the way we need.

    thanks

    //////////

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,900
    What is the point of having a function to write out the table or just table rows when you could just DO it in the while loop as you read the rows?

    Code:
    $con='';
    //  start the table
    //  build the column headings
    // now build the data rows
    while (row = mysqli_fetch)
    {
       $con .= '<tr>';
       $con .= '<td>'.row['col1']</td>';
       $con .= '<td>'.row['col2']</td>';
       $con .= '<td>'.row['col3']</td>';
       $con .= '<td>'.row['col4']</td>';
       $con .= '</tr>';
    }
    // close the table
    echo $con;
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,327
    Quote Originally Posted by ginerjm View Post
    What is the point of having a function to write out the table or just table rows when you could just DO it in the while loop as you read the rows?
    Two possible reasons (at least):

    The more compelling one would be if you generalize the function like I tried to do so that you can re-use it anywhere else you're going to output a table based on the result of a DB query. If this is the only place in this application, then it's not much of a reason, unless you expect to re-use it somewhere else eventually.

    The less compelling reason is a stylistic one, and depends a lot on personal or team preferences: moving detailed, fiddly stuff into functions so that the main entry-point of the code is easier to follow with regard to the general program flow. In that case, you might rethink the name of the function to make sure it's obvious what it's doing, so that when someone is reading the code they know what it does, and whether they either need to look at it because that's where they think a bug or change request would be addressed -- or that they can skip it because it has nothing to do with what they're looking for right now.

    For the latter reason, I have no strong feelings, other than when the code starts to look like the proverbial spaghetti, it's time to start modularizing things. For the former, though, I'm a strong advocate of D.R.Y. (Don't Repeat Yourself) code.

    And, of course, we could get all object-oriented about this, but that's for later.
    ___
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  9. #9
    Join Date
    Feb 2017
    Posts
    43
    Quote Originally Posted by NogDog View Post

    And, of course, we could get all object-oriented about this, but that's for later.
    ___
    I have a lesson today about OO Thus, it will be fun. I have started learning PHP last year in November and so far all that I have done is only college projects. I would like to work with PHP as I really love it but just like you said I'm writing a lot of spaghetti code. I know practice is the way how to improve my skills , howerver, I think that is not enough for me right now.

    thanks so far with all your help it does help a lot.

    ////////
    //////

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.19227 seconds
  • Memory Usage 2,978KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (5)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates