I am a student still new to php and mysql,i am developing a website which is database driven. It has two pages i.e index.php and subpage.php.
On the index page there appears 14 links namely about_us,projects,services,partners and among others. And for the case of the subpage.php, it has "a blank body",the header, navigation and the footer.
My goal is to not to create pages for every link that appears on the index.php. So i want to use only the "blank body" in the subpage.php to display the data for every link that is on the index.php whenever it is clicked on.
In my struggle to achieve this, i have created a database with 14 tables so that each should cater for every link on the index.php.
So I would like you guys to help me how i can RETRIEVE DATA FROM THE DATABASE FROM DIFFERENT TABLES ON TO THE SUBPAGE.PHP
Forexample;
If am am on the index.php and i click about_us, it should ONLY retrieve data from the table called about us in the database.
And if i click another link it should on retrieve data specifically for that link i have clicked on.
Here is the sample php codes that am using to retrieve data for only projects_table onto the subpage.php
<?php
require_once("connection.php");
?>
<table>
<?php
mysql_select_db("cognative_db",$sql);
$sql="SELECT * FROM projects_table ";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['project_title'];?></td>
<tr>
<td><?php echo $row['project_details'];?>
<?php echo " ";?>
</td>
</tr>
<?php
}
?>
<?php
mysql_close();
?>
</table>
Any help will be highly appreciated
I can't agree with your database design concepts. But to problem of having one page for all links , one way can be - you can create links like this
http://yoursite/subpage.php?page=content
http://yoursite/subpage.php?page=aboutus
On your subpage.php
$page= $_GET['page'];
depending upon the value of $page you can choose which query to run and what to display in the page.
To add to all these, you don't need to have 14 tables to cater 14 links. Perhaps you can start looking at Joins
simply create simple table structure for your website,like below one instead of assigning one menu one table
|id | menu | content |
|1 | about_us | asds.... |
|2 | services | asds.... |
I don't know why for every link you made the table. But just for displaying multiple table data, you can pass table name in query string as:
Click
// and on subpage.php retrieve it by get method as:
$table=$_GET['q'];
//and make your sql query
$sql="SELECT * FROM $table";
Related
I have a form set up, and I want the user to be able to enter information and then select via radio buttons which div the database will output the inserted data into. Right now I have it set up to where the user can input his information, and the information will be output into a div, but i'm trying to figure out how I would go about inserting data from the 1 database into different divs. For example:
The user inserts: Hello World
and it is input in to the database and displayed in div1, but then later the user decides to input "How are you?" and that be displayed in div2 and you would have something that looks like
| div1 | | div2 |
| Hello World | | How are you? |
Do I need to create 2 separate tables within my database that correspond with my respective div tags? What is the simplest way to go about this?
Use like this:
<?php
$result = mysql_query("SELECT * FROM table where column='column'");
while($row = mysql_fetch_array($result))
{
if($row['id']=="Hello World")
{
echo "<div id='div1'>".$$row['id']."</div>";
}
else if($row['id']=="How are you?")
{
echo "<div id='div2'>".$$row['id']."</div>";
}
}
?>
There are many ways to do this.
Perhaps the most logical and correct way is to have one table with the following columns: user_id, div_name, and text
Another way would be to have one table and a user_id, div1, and div2 table
As you mentioned, you could have two tables both with the structure user_id, text where the determination of which div it is for is in the table name but this is not good form and I'd suggest against it.
Currently I use SESSIONS to call for my varibles from a different page,where multiple data are retrieved from mysql database using a while() statement, onto another page. The many retrieved rows has a specific id which is retrieved from the table. Each row also has a link that when clicked takes you to the page 2 to show details of the person marching that id.
For instance on page1, these are the data shown
id | name
1 | Kamena
2 | aman
3 | Forfie
sample of my code on page 1
session_start();
$stmt="select id,name from table1";
$result=$db->query($stmt);
while($row=mysqli_fetch_assoc($result)){
$id=$row['id'];
$name=$row['name'];
$_SESSION['id']=$id;
echo "<table>
<tr>
<td><a href='page2.php'>id</a></td>
<td>name</td>
</tr>
</table>";
}
On page 2, this is a sample of my codes
session_start();
if (isset($_SESSION['id']))
$id=$_SESSION['id'];
$stmt="select id,name from table1 where id=$id";
$result=$db->query($stmt);
$row= mysqli_fetch_assoc($result);
$perID=$row['perID'];
$name=$row['name'];
The problem here is when the link, id which is 1 with Kamena been the name, is clicked to page 2 the id returns 3 on the page 2 mean while it suppose to return id which is equal to 1. Please is there is something I am doing wrong?
Yes you are writing to SESSION['id'] and overwriting it each time, thus storing the last value only. You need to do this...
$_SESSION['id'][$id] = $id;
This way you have access to it, and can access it as a key/value.
So on your next page if the id you want to access is 7, you would access it as such...
echo($_SESSION['id']['7']);
But a problem I see, is that you don't know which ID you clicked to be able to access on the next page. In your link you need to somehow reference it, then check if its in the session. Which kinda defeats the point of the SESSION variable anyways. Because then you could just pass it as a GET request
Like so,...
<a href='page2.php?id=7'>PAGE 2</a>
And on page two...
if(isset($_GET)){
$id = $_GET['id'];
}
$stmt="select id,name from table1 where id=$id";
$result=$db->query($stmt);
$row= mysqli_fetch_assoc($result);
This is completely unrecommended though, just highlighting the fact that you need to somehow pass it to the next page, and SESSION isnt the way since you need a way to reference that session somehow anyways. If you know what Im trying to say.
I am trying to populate 3 menus, 1st menu is created from mysql query and php and displays TVshows ( ie. Modern Family, Dexter, etc ), what I would like to do is once the TVShow is selected populate the next drop down with a new mysql query for seasons ( 1 ,2 ,3 etc.) , then populate a 3rd drop down via mysql query based off the first 2 options being selected for episode
The table is as follows
| id | Title | Season | Episode | Extension | URL
I can get the first drop down to display with the following code
<?php
$sql="Select distinct title from TVShows";
$result=mysql_query($sql);
echo "<select name='TVShow'><option value=''>Select TV Show</option>";
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[title]>$row[title]</option>";
}
echo "</select>";
?>
I have tried many examples but none seem to work right, I would like to be able to do this on the same page as opposed to having the user click submit to go to another page to select the second drop down.
I would like code to dynamically setup the 2nd dropdown based on the first choice, then dynamically setup the 3rd dropdown based on the 1st and second dropdowns
mysql_fetch_array does not fetch an associative array.
Try using mysql_fetch_assoc instead
For better performance on the user end you might consider loading all results from the db (and then ideally caching them) and then hiding and showing the appropriate ones with javascript.
This way if users are clicking on your menu a lot you're not making a ton of unnecessary round trips to the db.
Ok i have a dynamic page where people can post events in the city, we will call that page: city.php. In order to get to the page though, you must select a state from states.php, then a city from allcities.php. The states and cities are all in mysql database. On the city.php page you can click "add event" and it will take you to createevent.php where you can create and add an event that shows on city.php. But here is what i want to do:
I want to make it so that city.php is the central spot for posting different things for that city. I want a page for events, news, jobs, and for sale. On the city.php you will select a link for those pages taking you to pages such as events.php, news.php, jobs.php, and sale.php. How can i keep all those pages dynamic and related to the selected city? I toyed around and couldn't figure it out.
There are a number of ways to do something like this in PHP. I personally disagree with your method as it sounds. I could be wrong about what it seems like you're saying though.
As adam said, $_SESSION is the first thing in mind. You can also look into $_COOKIE:
http://php.net/manual/en/features.cookies.php
or just pass the city id as a $_GET var:
http://www.yoursite.com/events.php?city=2
Then read it in PHP like
$city_id = $_GET['city'];
I'm not sure if I understand you, but do you want variables to cross over into other pages? Take a look at PHP's use of sessions. Sessions are very handy, it essentially allows you to have site-wide variables associated to each of your users.
http://php.net/manual/en/features.sessions.php
Basically at the top of each of your pages just put:
session_start();
And then to assign / access a session variable you just call:
$_SESSION['city'] = $city;
or
echo $_SESSION['city'];
Quite generic but key is in the relational db structure. Displaying is easy, so make sure that news, job, events tables has id_city for building correct queries.
Example DB style as answer to your comment:
cities : id_city | city_name | city_slug | id_state
events : id_event | id_city | event_name | other_event_data | ...
jobs : id_jobs | id_city | job_name | job_salary | etc.
So user clicks to yoursite.com/washington
Then you can query that comes from url (here is washington) like:
SELECT id_city FROM cities WHERE city_slug = "washington"
You got id_city now. Then if it is jobs,
SELECT * FROM jobs WHERE id_city = "above gotten id_city"
Hope this helps.
I've seen alot of tutorials on search with php and mysql, but im having trouble with generating a link with the search result. For example say i have an item called "item1" in my db and the user searhes for item , item1 should be returned as a link so the user can click the link to get more information about that product. Does anyone have any scripts , or snippets of code for how to acheive this?Thanks
You probably need to create an item page say item.php that accepts an id which will then search teh database for that item and display the item information.
Your search results will then have to display the name of the item in a link that also includes the id of the item.
<?php echo $itemname"?>
This would of course be in a loop that goes through the list of items one at a time.
When the user clicks the link it will take them to item.php and send id as a parameter.
that's a simply example with no check and no control by datatype or query results. just start from it and do what you need...
<?php
//your db connection
//col1 is where the id is saved
//col2 is the url
$qry="SELECT col1, col2 FROM table WHERE col1='".$_GET['var']."'";
$result=mysql_fetch_array(mysql_query($sql));
echo 'LINK';
?>
edit:
if you want a direct redirect do this instead of echoing
header('Location: '.$result['col2']);