Jump to content

Recommended Posts

What I'm hoping to do is , when an <a> tag is click  it will send a text value to php to change whatever name is assigned on the $table variable. To give an example. On the side are all the branches this clinic has. Instead of creating new php file for each of the branch ( with the same design and functionality) and href it accordingly, once click it will send a text to replace the default value of $table. I think AJAX will help do the trick but I'm new to it. Please help me, I'm so lost. 

Below is my code that displays all the branch, and what it looks like. 

<div class="BNavcontainer" >  
			<div id="branchnav" class="SideBNav">
			<?php
			/* To display Branch Name from djams_db */
						$brnch = $conn->query("SELECT * FROM branches") or die($conn->error);
						  while($row=$brnch->fetch_assoc()):
			?>
		    	<a data-id='<?php echo $row['brnch_id'] ?>' href="djams_rmc.php?<?php echo $row['brnch_name'] ?>" 
				  	id="b<?php echo ($row['brnch_id']) +1 ?>" >
				  	<?php echo ($row['brnch_name']) ?>
				</a>
		    <?php endwhile; ?> 
			</div>
		</div>

   image.png.ea610e320714a2d0c0f4c8a4e8e34a88.png

Edited by Elara
  • Elara changed the title to onclick of an html <a> tag change what $table will be used

Had a difficult time reading your code so did this re-write to make it clear.  Not sure if I left something out but there was a "?>" part that didn't make sense.

echo "<div class='BNavcontainer'>
	<div id='branchnav' class='SideBNav'>";
/* To display Branch Name from djams_db */
$brnch = $conn->query("SELECT * FROM branches") or die($conn->error);
while($row = $brnch->fetch_assoc())
{
	echo "<a data-id='" . $row['brnch_id'] . "' href='djams_rmc.php?" . $row['brnch_name'] . 
			"' id='b(" . $row['brnch_id'])+1 . ")'>" . $row['brnch_name'] . "</a>";
}
echo "</div></div>";

So - yes you are displaying a set of anchor tags for each row of query results.  These will send control to another script.  Don't see why you use the word AJAX, especially since you are a noob.  What do your want your screen to do for the user?

Edited by ginerjm

Please ignore the code in the first post - it needed some corrections but I took too long to get them in.  Here are my corrections.

echo "<div class='BNavcontainer'>
	<div id='branchnav' class='SideBNav'>";
/* To display Branch Name from djams_db */
$brnch = $conn->query("SELECT * FROM branches") or die($conn->error);
while($row = $brnch->fetch_assoc())
{
	echo "<a data-id='{$row['brnch_id']}' href='djams_rmc.php?{$row['brnch_name']}' 
				  	id='b(" . $row['brnch_id'] +1 . ")'>({$row['brnch_name']})</a>";
}
echo "</div></div>";

 

I'm sorry if my code is confusing. To make myself clear, I'm making an appointment management system. here is my index.php

image.thumb.png.c973358525068a86fd1ad75115e54def.png 

this is unfinished and supposedly in each date  an event will be displayed. 

The colorful things on the left side is a navigation to direct the user to a specific branch.  But each branch page would look the same but will use different tables from my database to display events set on that branch. That's why I mentioned ajax, for whichever is click from that navigation I was aiming that it will load the data from a specific table. 

The reason why I put my html code mixed with php is to give an idea of how my navigation works. 

I'm not sure if this made it clear, but I'll try my best to explain clearer as to what I'm working on. Thank you 

What is the ajax going to do for you?  What is the code you did show us supposed to do?  I was picturing some kind of menu to allow your user to make a selection of branch and go to a page associated with that branch.  The colorful thing you are displaying here is not part of that in my mind.

Have you used ajax before?

what you were picturing was correct, ,  It was a menu to chose the branch but instead of going to another page I would like to reload the current page but it carries a different table name associated with that branch for me to use in  selecting from database. The colorful thing is the design of my menu, instead of a usual lined -up menu as per our capstone client request.  

I have used ajax in this same project too, but I can't get it to work correctly. Correct me If I'm wrong but to be able to pass a JS variable to PHP variable ajax is used right?  and I also assumed that as I will be using an onclick the name of the branchs' table will be stored in js first and then will be pass to $table variable in php. 

$brnchTbl = 'alabang';
	 //Display Data   
	 $disData="SELECT *,concat(px_fname,' ', px_lname) as px_name, concat(px_contact,' / ', px_contact2) as px_connum FROM appt_$brnchTbl"; 

 I was planning to use this code to fetch data from database, but the $brnchTbl value will change based on what is clicked in the branch menu. 

 and the first code I showed generates the colorful things on the side. 

Edited by Elara

You are trying to create a menu that will not use the user's choice to go to another page?  Makes no sense.  Why, after the pick, would the user want to return to the same page?

And why is JS coming up in our current conversation since you are not using it?

Perhaps you need to focus on just one thing now.  I suggest looking at the anchor tags you are creating and getting them correctly formed.  Run your script (with my changes?) and then view it in your browser's display mode where you can view the actual html code and see what you've built.

I'm not sure of what term I should use, and I'm sorry if my English is not good. I'll try to explain it again. 

what I'm trying to achieve is: 

index.php is a page wherein it will retain the same layout and design but will display different content base on what branch was chosen by the user. The colorful elements on the left side is where the user would chose the branch, once a branch is chosen the page will reload but with different content that will come from the table associated with each branch I created in my database. But I don't how am I suppose to pass the table name of the chosen branch to my select query. 

 

I'm sorry if I confused you. That script for the menu was working , I think I misled the question with that script, I only showed it as I thought it would be  the starting point for what I was aiming for. Still thank you for trying to help me.

 

Edited by Elara
11 minutes ago, Barand said:

Why is the data for the different branches in different tables? Is the structure of the data different for each branch?

They have the same structure but I thought I should separate the table per branch so that it will load faster , am I wrong ? 

Have one appointment table for all branches. Add an extra column (indexed) for "branch_id" so you know which branch each row belongs to.

Then all your query needs is "WHERE branch_id = ?" and no need to switch tables.

                                     +-----------------+ 
                                     |  branch         | 
                                     +-----------------+ 
                              +------|  branch_id      | 
                              |      |  branch_name    | 
+------------------+          |      +-----------------+       
| appointment      |          |
+------------------+          |
| app_id           |          |
| branch_id        |>---------+
| app_date_time    |           
| description      |           
| etc              |
+------------------+

 

28 minutes ago, Barand said:

Have one appointment table for all branches. Add an extra column (indexed) for "branch_id" so you know which branch each row belongs to.

Then all your query needs is "WHERE branch_id = ?" and no need to switch tables.

                                     +-----------------+ 
                                     |  branch         | 
                                     +-----------------+ 
                              +------|  branch_id      | 
                              |      |  branch_name    | 
+------------------+          |      +-----------------+       
| appointment      |          |
+------------------+          |
| app_id           |          |
| branch_id        |>---------+
| app_date_time    |           
| description      |           
| etc              |
+------------------+

 

I have a similar tables  to what you showed. but how am I going to get ? in "WHERE branch_id = ?"  base on what menu was chosen by the  user

Edited by Elara
27 minutes ago, Elara said:

I have a similar tables  to what you showed. but how am I going to get ? in "WHERE branch_id = ?"  base on what menu was chosen by the  user

image.png.56e7545d194c4f017fefeb49c2cd42ec.png

 

I already have echoed their ids from db,  but I'm not so sure how I'm going to make store their ids to use it as ? in  "WHERE branch_id = ?" where ' ? '  should be equals to the data-id of this a tags. 

I hope my question is not confusing you. 

 

Edited by Elara

Your links would be in the form

<a href='djams_rmc.php?branch=X'>

where X is the id of the branch.

Then, in your appintments page (djams_rmc.php), you get the required branch with
 

$id = $_GET['branch'];

$query = $db->prepare("SELECT whatever FROM appointment WHERE branch_id = ?");

$query->execute([ $id ]);

 

  • Thanks 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.