Hi
I am trying to display this design pattern(a triangle of asterisks) in my screen-
<t> *
<t> * *
<t> * *
<t> * *
The pattern may be not coming so please see the attached bmp image to get an idea.The stars are simple asterisks in keyboard.
Problem is I tried very hard with this code I wrote but it is not giving the desired result!!
Please tell me what is wrong with the code and can you give me a full working code?
Since I am a noob I may be using many unnecessary variables while experimenting, like $c above .Please change the code if necessary but please give me a full program code.
Will be greatly obliged.
Hi jmj001, Great!! I liked your answer and I thanked you too for the great help.
But ,yes .I am looking for variable input , that is ,I want user to input the number of rows and then the output in the form of a triangle of stars with the specified no. of rows will be displayed.
Also,I will request you to give me in the same format as the code I posted.I mean can you pls tell me how to display with loops only without using PHP array?
Or if you can give me in "C" Language that will also be fine.
Whichever I would request you not to the format I have posted ,i.e. ,pls use the loops (whatever u can use -while,for,if-else without using any advanced features of php or any other language except C programming as I said before)!!
Thanks again.
Hope you will help me.
This will allow users to input any number of rows, then display the asterisks accordingly.
It only needs two for loops. One to iterate through the rows, and one for the stars of each. The style is controlled with CSS, rather than using a billion .
Edit: Just updated as used span instead of div by mistake
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head> <title>Stars</title>
<style type="text/css">
div { text-align: center; }
.ast { padding: 5px; display: inline; }
</style> </head>
<body>
<form method="post" action=""> Select number of rows <input type="text" size="2" name="rows" /> <input type="submit" value="Go" />
<div> <?php if (isset($_POST['rows'])) { $rows = $_POST['rows'];
if ($rows == "") echo 'You didn\'t enter anything!'; else {
Hi Dan-dan,
Problem is you perhaps didn't see the picture I attached!!
It is a hollow triangle and in your case it is a filled one!!
Please see the attached bmp image .
Good Luck!!
Also,if it is possible you can also write in C language.
But still I have a few questions @dan-dan.
Firstly,how u fix how many pixels are needed to give the spaces?
2ndly,what is the spacer doing when there is padding?
3rdly,why r u giving padding to the asterisks?
4thly,can u tone down ur porgram to a more simpler one like the one I wrote?
I mean without using CSS or Javascript or any other language ,can u write the program in the way I wrote originally in PHP only?(without using any advanced features of PHP of course like no arrays)
1) The below loop works out how many points are needed for a full triangle, then echos an astrisk for the first and last points, and empty spans for the points in between. Bare in mind I originally thought it was a filled triangle. The class 'spacer' specifies the width of the spacer from the CSS. I just changed the number till it looked right.
2) The padding was to create space in between the astrisks. Like said above the spacer was to replace what were the filling astrisks.
3) So they're not squashed together.
4) This isn't complex in the slightest! It isn't using any advanced features either. In fact it uses the same loops as yours. You can get rid of the CSS and just use HTML, but why take a step back?
Also, you're not just using PHP, you're also using HTML within your PHP. HTML and CSS work hand-in-hand, so it might be worth learning.
I'm not going to write the program again, I'm sure you can manage the tweaking.
Dear dan-dan,
I do know CSS nd HTML,but actually for some reason I want it in PHP(& of course HTML but no CSS) only.A program as u very well know can be written in HTML and PHP only without using any CSS at all.then why dont u write a program without CSS for my sake? Actually I originally want to write a C program so I need the loop logic clearly ,of course CSS is now a regular practice but as I aid for the above reason I want only in HTML and PHP only.
Hope u now got me!!
Now sorry ,I cannot manage the tweaking ,can you do me that one last favour pleaseee?