Artificial Intelligence Programs For Programming in Python

Traveling Salesman Problem ( TSP )] Artificial Intelligence Programs For Master Leval Programming in Python from iter tools import permutations def distance(point1, point2): >>> distance([3,4],[0,0]) 5.0 >>> distance([3,6],[10,6]) 7.0 return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) ** 0.5 def total_distance(points): all the points are in the given order. >>> total_distance([[1,2],[4,6]]) 5.0 >>> total_distance([[3,6],[7,6],[12,6]]) 9.0 return sum([distance(point, points[index + 1]) for index, point in enumerate(points[:-1])]) def travelling_salesman(points, start=None): Time complexity is O(N!), so never use on long lists. >>> travelling_salesman([[0,0],[10,0],[6,0]]) ([0, 0], [6, 0], [10, 0]) >>> travelling_salesman([[0,0],[6,0],[2,3],[3,7],[0.5,9],[3,5],[9,1]]) ([0, 0], [6, 0], [9, 1], [2, 3], [3, 5], [3, 7], [0.5, 9]) """ if the start is None: start = points[0] return min([perm for perm ...

How to create Pagination with PHP and MySql - Codingpoint

How to create Pagination with PHP and MySql 

How to Create Pagination in PHP
Pagination in Table PHP

1. Create a Database and connect it.

<?php
error_reporting(0);
$mysql_hostname = "localhost"; //your mysql host name
$mysql_user = "root"; //your mysql user name
$mysql_password = ""; //your mysql password
$mysql_database = "php_advance"; //your mysql database


$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Error on database connection");
?>


2.index.php new file

<?php
include('config.php'); //include of db config file
include ('paginate.php'); //include of paginat page

$per_page = 5; // number of results to show per page
$result = mysql_query("SELECT * FROM countries");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval($_GET['page']);


$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagination With PHP</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<style type="text/css">
.logo
{
text-align: center;
}
.container{


}
</style>
</head>
<body>
<div class="container">
<!-- <div class="row">
<div class="logo">
<a href="http:/www.techumber.com">
<img src="img/logo.png" alt="techumber.com logo"/>
</a>
</div>
</div> -->
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>country code</th> <th>Country Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}

// echo out the contents of each row into a table
echo "<tr " . $cls . ">";
echo '<td>' . mysql_result($result, $i, 'ccode') . '</td>';
echo '<td>' . mysql_result($result, $i, 'country') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
</div>
</div>
</div>
</body>
</html>


3.Paginate.php

<?php

function paginate($reload, $page, $tpages) {

$adjacents = 2;

$prevlabel = "&lsaquo; Prev";

$nextlabel = "Next &rsaquo;";

$out = "";

// previous

if ($page == 1) {

$out.= "<span>" . $prevlabel . "</span>\n";

} elseif ($page == 2) {

$out.= "<li><a href=\"" . $reload . "\">" . $prevlabel . "</a>\n</li>";

} else {

$out.= "<li><a href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n</li>";

}



$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;

$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;

for ($i = $pmin; $i <= $pmax; $i++) {

if ($i == $page) {

$out.= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";

} elseif ($i == 1) {

$out.= "<li><a href=\"" . $reload . "\">" . $i . "</a>\n</li>";

} else {

$out.= "<li><a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n</li>";

}

}



if ($page < ($tpages - $adjacents)) {

$out.= "<a style='font-size:11px' href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";

}

// next

if ($page < $tpages) {

$out.= "<li><a href=\"" . $reload . "&amp;page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n</li>";

} else {

$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";

}

$out.= "";

return $out;

}

Comments

Popular posts from this blog

MongoDB Exercises Practice Solution Exercise First

Simple Basic Commands For MongoDB

Artificial Intelligence Programs For Programming in Python