Question

Use php Create a file to write a program of 30 city name and populations Using...

Use php
Create a file to write a program of 30 city name and populations
Using two files
Print using associative array

Output should be like
Cities:
Name. Population
Daka. 13 millions

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1st code : a form to add data to files

code:

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
   <form method="post" action="">

       <input type="text" name="cities" placeholder="city name"> <br><br>
       <input type="text" name="population" placeholder="population in millions"> <br><br>
       <button type="submit" name="submit">submit</button>
   </form>
</body>
</html>

<?php

if (isset($_POST['submit']))
{
   $v1=$_POST['cities'];
   $v2=$_POST['population'];
   $f1=fopen("country.txt","a");
   $f2=fopen("population.txt","a");
   fwrite($f1,$v1." ");
   fwrite($f2,$v2." ");
   fclose($f1);
   fclose($f2);
}
?>

output:

city name population in millions submit

2nd code: to read files .. store in associative arrays and display it .. i have taken 5 values .. it will work for 30 as well

code:

<?php
$f1=fopen("country.txt","r"); //open file in read mode
$f2=fopen("population.txt","r"); // open file in read mode
$data=array(); // declaring a associative array
while (! feof($f1)) // inserting values in associative arrays
{
   $data+=array(fgets($f1)=>fgets($f2)); // this will continue untill one of the file reaches its end
}

fclose($f1); // closing the files
fclose($f2);
echo ("cities"."<br><br>");
?> <table border="1px"> <?php // creating a table
//echo ("name".'&nbsp&nbsp&nbsp'."populations"."<br><br>");
echo ("<tr><th>"."city"."</th>"."<th>"."population"."</th></tr>"); // printing values
foreach ($data as $key => $value)
{
   if ($value != 0)
   {
       echo"<tr><td>".$key."</td>"."<td>".$value."</td></tr>"; // printing values in associative array
   }
   //echo $key.'&nbsp&nbsp&nbsp'.$value."&nbsp millions"."<br>";
}
?></table><?php

?>
output:

cities city population kolkata |10 dhaka50 kabul 25 mumbai 19 khulna 5 34

thumbs up if u like and comment for any issues

Add a comment
Know the answer?
Add Answer to:
Use php Create a file to write a program of 30 city name and populations Using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Print out information of PHP use phpinfo() function. 2. Write a program that check and...

    1. Print out information of PHP use phpinfo() function. 2. Write a program that check and print odd / even numbers (from number 1 to 100 using for/while loop). Display the results within an HTML table with 2 columns as shown below: NUMBERS RESULTS 1 ODD 2 EVEN 3 ODD HINT: use <table> tags to create a table, <th> tags for ‘Numbers’ and ‘Results’. Wrap code PHP inside HTML code. For example: ​<html> ​​<title>CHECK ODD or EVEN</title> ​​<body> ​​​<table> ​​​​<?php...

  • Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You wi...

    Homework IX-a Write a program that opens a text file, whose name you enter at the keyboard You will be using the file text.txt to test your program. Print out all of the individual, unique words contained in the file, in alphabetical order Print out the number of unique words appearing in text.txt. Call your program: YourName-HwrklXa.py Make sure that your name appears as a comment at the beginning of the program as well as on the output display showing...

  • Create a folder named "TrainerApp". In that folder create the following files. Create a PHP file,...

    Create a folder named "TrainerApp". In that folder create the following files. Create a PHP file, "insert-user-form.php", with a form that has the following fields: - First Name (text) - Last Name (text) - Email (text) - Password (text) - Submit button Create another PHP file, "insert-exercise-form.php" with a form that has the following fields: - Exercise Name (text) - Description (text) - Demonstration Image (file) - Submit button Create another PHP file, "login-user-form.php" with a form that has the...

  • Write a program that asks the user for the name of an image, the name of an output file. Your pro...

    Write a program that asks the user for the name of an image, the name of an output file. Your program should then save the upper right quarter of the image to the output file specified by the user. A sample run of your program should look like: Enter image file name: hunterLogo.png Enter output file: logOUR.png which would have as input and output: HUNTER ITER The City University of New York Hint: See sample programs from Lectures 3 and...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • This code should be written in php. Write a program that allows the user to input...

    This code should be written in php. Write a program that allows the user to input a student's last name, along with that student's grades in English, History, Math, Science, and Geography. When the user submits this information, store it in an associative array, like this: Smith=61 67 75 80 72 where the key is the student's last name, and the grades are combined into a single space-delimited string. Return the user back to the data entry screen, to allow...

  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

  • Read a file called populations.txt and populate a php associative array with the values read from...

    Read a file called populations.txt and populate a php associative array with the values read from the file. Here is an example populations.txt file: Dhaka, 23 million Bangaluru, 21 million New York, 9 million Emporia, 23 thousand Here's some pseudo code: open the file loop through the lines in the file read a line into a string variable explode (or is it split, or something else) the string into key value pairs save the key value pair into your associative...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT