Jump to content

Php function call in html stops loading footer or loads below footer


Recommended Posts

Hi All,

I am a newbie to php and I have this issue. I would appreciate if anyone can help out. I tried a few things but was not able to come up with a solution.

From reading few codes and learning from it, I created this function in query.php and am calling this function in uk.php file, but the problem is that it loads and shows the data in table, but then it stops loading page from there on and the footer which is in html code after the place where I am calling this function does not load.

I tried removing return false; command, but with that it loads the footer, but it shows the table after the footer.

I want the table with data to show in between header and footer.

Can you guys let me know what I need to do to achieve that?

File Name: query.php

 

function uk()
{
	$output = '';
	$result = db_query("SELECT * FROM lecture where groups='uk'");

	if(mysqli_num_rows($result) > 0)
	{
		$output .= '<div id="style2" style="overflow-x:auto;">
						<table>
							<tr>
								<th class="text-center">Date</th>
								<th class="text-center">Title</th>
								<th class="text-center">Venue</th>
								<th class="text-center">Duration</th>
								<th class="text-center">Size (MB)</th>
								<th class="text-center">Link</th>
							</tr>';
		while($row = mysqli_fetch_array($result))
		{
			$output .= '
				<tr>
					<td>'.$row["mydate"].'</td>
					<td>'.$row["title"].'</td>
					<td>'.$row["venue"].'</td>
					<td>'.$row["duration"].'</td>
					<td>'.$row["size"].'</td>
					<td><a href="../'.$row["path"].$row["file_name"].'">Save</a></td>
				</tr>
			';
		}
		echo $output;
	}
	else
	{
		echo 'Data Not Found';
	}
}

 

File Name: uk.php

 

Header html code here

Some body text

<?php
include '../includes/query.php';
uk();
return false;
?>

Footer html code here

 

 

A function also known as a method when in a Class should do one thing and do it well. It should also be easily re-usable and not have to be edited to use it elsewhere. What you have is none of that.

What you will probably discover as the first problem is that you have hard coded the groups which means if you want a group other than uk you now have a problem on your hands. Duplicating the function for every group is not the answer.  What you want to do is pass the groups value to the function as a parameter. Now you can use the same function for any group. Same thing goes for the hard-coded table name. There is also no option to specify specific columns. Again you would run into problems with the hard coded column headers and every other hard coded item.

Here is something you get you going towards a dynamic solution. (It's not a cut n paste solution)
 

<?php declare(strict_types=1);

$sql = 'YOUR-QUERY-HERE';
$stmt = $db->pdoQuery($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$row)
{
    echo '<H1>No Records Available</H1>';
}
else
{
    $data = [];
    foreach ($row as $key => $val)
    {
        $data[ucwords(str_replace('_', ' ', $key))] = $val;
    }
    ?>
    <div class="table-responsive">
        <table id="myDataTable" class="table table-bordered table-striped table-hover table-sm">
            <thead class="thead-dark">
            <tr>
                <th><?= join('</th><th>', array_keys($data)) . "</th>\n" ?>
            </tr>
            </thead>
            <tbody class="searchable">
            <?php
            do
            {
                echo "<tr>\n<td>" . join("</td>\n<td>", $row) . "</td>\n";
                echo " </tr> \n";
            } while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
            ?>
            </tbody>
        </table>

    </div>
    <?php
}

 

 

Edited by benanamen
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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