Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Created December 20, 2023 12:06
Show Gist options
  • Save adeel-raza/4f314384d39b35613fe458daafd97dbe to your computer and use it in GitHub Desktop.
Save adeel-raza/4f314384d39b35613fe458daafd97dbe to your computer and use it in GitHub Desktop.
Insertion sort contact list with PHP
<!--
Pusedo Code:
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Coding Arena</title>
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,600,700" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<style>
* {
font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen-Sans", Ubuntu, "Cantarell", "Helvetica Neue", sans-serif;
}
body {
font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen-Sans", Ubuntu, "Cantarell", "Helvetica Neue", sans-serif;
background-color: #f2f2f2;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
max-width: 600px;
}
h1, h2, h3 {
color: #333;
}
div {
background-color: #f5f5f5;
padding: 5px;
color: #4f4f4f;
border: 1px solid #ddd;
border-radius: 3px;
display: block;
}
/* Style input fields and buttons */
/* Add more styles as needed */
</style>
</head>
<body>
<div class="container">
<h1>My Coding Arena</h1>
<p>The code shows here:</p>
<div>
<!-- Add your code below this line -->
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$contact_info = array(
'Zeeshan ' => ' 03332490820',
'Bilal ' => ' 03332490895',
'Sami' => ' 03332490165',
'Ahmed' => ' 03332490090',
' Batool ' => ' 03332490554',
' danish ' => ' 03332490673',
'Owais ' => ' 03332490967',
'ahsan ' => ' 03332490534',
'ehsan' => ' 03332490899',
' Imtiaz ' => '03332490678',
);
function insertion_contact_info( $contact_info ) {
$no_of_contacts = count( $contact_info );
for ( $steps = 1; $steps < $no_of_contacts; $steps++ ) {
for ( $key = $steps; $key > 0; $key-- ) {
if ( strcasecmp($contact_info[ $key ] , $contact_info[ $key - 1 ] ) < 0 ) {
$tmp = $contact_info[ $key - 1 ];
$contact_info[ $key - 1 ] = $contact_info[ $key ];
$contact_info[ $key ] = $tmp;
}
}
}
return $contact_info;
}
// Remove whitespace from array keys and values
foreach ( $contact_info as $name => $contact_no ) {
$name = trim( $name );
$contact_no = trim( $contact_no );
$formatted_contact_info[ $name ] = $contact_no;
}
$contact_names = array_keys( $formatted_contact_info );
$sorted_contact_names = insertion_contact_info( $contact_names );
foreach ( $sorted_contact_names as $key => $name ) {
unset( $sorted_contact_names[ $key ] );
$sorted_contact_names[ $name ] = $formatted_contact_info[ $name ];
}
?>
<h3> Sorted Contact List </h3>
<?php foreach ( $sorted_contact_names as $name => $contact_no ) : ?>
<p>
<?php echo $name . ' : ' . $contact_no; ?>
</p>
<?php endforeach; ?>
<!-- Add your code above this line -->
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment