Repeat link with $_GET - php

Fairly new and inexperienced with PHP, and I'm stuck.
I have a feature that creates a new sub page that is related to a main page, eg. main page 1 has sub page 1 a, 1b and 1c, and main page 2 has sub page 2a, 2b, 2c and so forth. You gain access to the given sub pages through links in a menu on each of the main pages. I hope you're still with me here.
I have succeeded in getting the menu to link to one of the created sub pages, but what I now need is to get a link dynamically created in the menu whenever I create a new sub page. Main pages and their "id" is connected to the related sub pages via the column "mainid" that corresponds to eachother (eg. index.php?id=5&mainid=5)
The link menu is created by using $_GET.
I have this page: localhost/site/index.php?id=1&mainid=1 with the following code that i'm struggling with:
<table width="90%" border="0" cellpadding="5"><tbody>
<tr>
<td>
<?php
$id = $mysqli->query("SELECT id FROM page_content WHERE mainid =" . $_GET['mainid'])->fetch_object()->id;
$name = $mysqli->query("SELECT name FROM page_content WHERE mainid =" . $_GET['mainid'])->fetch_object()->name;
?>
<a href="index.php?id=<?php echo $id; ?>&mainid=<?php echo $_GET['mainid']; ?>">
<?php
print $name;
?></a></td>
<td> </td>
</tr>
This creates a link to content of the sub page. The content of this sub page is stored in id=10 and mainid=5 (thus creating the link index.php?id=10&mainid=5
So far so good.
But how do I get links to be dynamically created on a main page whenever I create a new sub page belonging to that given main page?
Help is much appreciated
Thanks

I'm not sure what you are tying to do, but i guess you want to make a loop from database.
To do that, you have to modify your code because right now is not safe, and not good.
use this one instead:
<table width="90%" border="0" cellpadding="5"><tbody>
<tr>
<?php
/* create a prepared statement */
$query = $mysqli->prepare("SELECT id,name FROM page_content WHERE mainid = ?");
/* bind parameters */
$query->bind_param("i", $_GET['mainid']);
$query->execute();
$menu_result = $query->get_result();
/* now you can fetch the results into an array */
while ($menu = $menu_result->fetch_assoc()) {
echo <<<HTML
<td>
{$menu['name']}
</td>
<td> </td>
HTML;
}
?>
</tr>
</tbody>
</table>

Related

How do I pull variables from my Database?

I have a menu of Home About Blog Templates Contact.
There is a table in my database called Categories.
The items in my table are Home About Blog Templates Contact.
These five items are being pulled from my database by my header.php file for my website.
What I want is to be able to click on each category and it display a specific .php file I have.
for instance if I click home it will display my index.php
clicking about displays my about.php
clicking blog displays my blog.php file etc.
I am not sure how to go about this ?
I am following a tutorial on Udemy as a guideline but, I need further assistance.
This is the php code at the top of my header.php file
<?php
include("includes/config.php");
include("includes/db.php");
$query = "SELECT * FROM categories";
$categories = $db->query($query);
?>
This is the code that is pulling my menu from the database
<?php if(isset($_GET['category']) || strpos($_SERVER['REQUEST_URI'] , "index.php") === false) { ?>
<a class="" href="index.php">Home</a>
<?php }else { ?>
<a class="" href="index.php">Home</a>
<?php } ?>
<?php if($categories->num_rows > 0) {
while($row = $categories->fetch_assoc()){
if(isset($_GET['category']) && $row['id'] == $_GET['category'] ){ ?>
<a class="" href="index.php?category=<?php echo $row['id']; ?>"><?php echo $row['text']; ?></a>
<?php } else echo "<a class='' href='contact.php?category=$row[id]'>$row[text]</a>";
} }?>
Essentially, what is going on in this code is when I click home it displays my index.php file and the code in my index.php file display code from another table I have.
However, when I click the about blog templates and contact categories it will only display the contact.php file for each category I have.
Thus, when I click About Blog Templates Contact only the Contact.php file displays.
I need a starting point on how to build on this where I can tell the code if about, blog, templates, contact is click on then display it's respective file.
The question is how do I use the categories in my database as my variables.
I was thinking using else if statements such as else if categories is = about then display. about.php
else if categories is = blog then display blog.php
If there are any free resources or books I can use to figure this out it would be much appreciated.
EDIT
The Table has only is called categories
ID Text
1 About
2 Blog
3 Templates
4 Contact
There no data stored in the table. All the table structure is ID & Text. The Text is being displayed as the menu and each Text has its associated ID. To be clear the HOME button is apart of the menu but, its not in the category table. I hope this is enough information.
Where you have this in the loop's else block:
} else echo "<a class='' href='contact.php?category=$row[id]'>$row[text]</a>";
Note the href has contact.php for all links.
What I believe that your trying to achieve, based on the categories, is:
} else echo '<a class="" href="' . $row['text'] . '.php?category=' . $row['id'] . '">' . $row['text'] . '</a>';
...so that you set the link's file name to match the category name in the link?

How to use DOM object for Current PHP page

In my php page I am getting a list of cities and events dynamically from a database. Then I'm passing this sql data to jquery to render a table with the data. Sample table created by jquery could be like:
<tr>
<td id='cityMumbai'>Mumabai</td>
<td id='program1Start>11/11/11</td>
<td id='program2Start'>11/15/11</td>
<tr>
Now I've come to know that DOM can be used to manipulate table structure in php.
I saw various reference, and found it is being used to create a new page. Means the created paged is saved physically.
Possible Pseudo Code:
{
<span id="eventRows">
<? $object = new DOMDocument;//it should pick this specific instance ?>
<? while($row = mysql_fetch_array($rProgList,MYSQL_ASSOC)):?>
<? // write code to check if current cityname exitst?
$td = $object->getElementById($row['cityname']);
if(!$td.exists): ?>
<tr><td id="<? echo $row['cityname'];?>"><? echo $row['cityname'];?></td></tr>
<? else : ?>
<?
//code to insert only event information in the current city object
$td.append("dfkjsdflkjasdhflkjsfh");
?>
<? endif; ?>
<? endwhile; ?>
</span>
}
Can I use DOM in PHP to modify the page output, only... replacing the task jquery is doing using PHP. Not to modify a physical copy of it.

how to split introtext in joomla?

I developed a module which can display article details as newsfeed. By clicking that article title user can go to the full article link.
this newsfeed display in main page. but it display the full content of the article.
I want to display only the first paragraph and if there are images it also.
I used here joomla content table introtext column data.
But i want to only display first paragraph and images only.
This is the code i used.
<?php
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM #__content WHERE catid = 8");
$fullArticle = $db->loadObjectList(); ?>
..........
<?php foreach($fullArticle as $article){ ?>
<ul>
<li>
<h2><?php echo $article->title; ?></h2>
<?php echo $article->introtext;?>
</li>
</ul>
<?php } ?>
can anyone help me?
You need to put a readmore into the article itself, or joomla considers the entire article is introtext. This is where you'll find the read more button which you can split your articles with, in the article editor:

php echo not working in html

This is my first code in php, so my problem might be so obvious. sorry if it is so easy :)
What I'm trying to do is that I am selecting some rows from my data base using
$rrows = Select ( "*" , $tbl_SubForum , null, "p");
$rrows->setFetchMode(PDO::FETCH_CLASS, 'subForum');
I know this works fine.
Each row has the description of a sub forum, containing title and id. I am trying to show sub forum titles in table cells using this code:
<table cellpadding=50px cellspacing=20px BORDER=0>
<?php
$i=0;
while($rrow = $rrows->fetch()){
var_dump($rrow);
?>
<tr>
<td class='subforum' id='subforum1'>
<?php echo $rrow["title"]; ?><br>
Sub forum manager<br>
Posts: 200<br>
Active users: 50<br>
</td>
</tr>
<?php
$i++;
}
?>
the line echo $rrow["title"]; doesn't work and so the page is empty, except for the result of the first var_dump
First var_dump of the first $rrow shows:
as you can see, there actually is a title field in the array and there is only one var_dump so the while loop doesn't work anymore!
why is this happening?
Because $rrow is an object rather than an array, you have to use $rrow->title to access its data member.

Dynamically display the result of a MySQL query into an HTML page

I use the YII Framework and I would like to put the results of a MySQL query in a table in index.php.
The MySQL query is already good:
SELECT categories.name,
systemes.name,
systemes.etat_de_base,
maintenances.name,
maintenances.date,
maintenances.duree
FROM systemes_maintenances
LEFT JOIN systemes
ON systemes_maintenances.id_systemes = systemes.id_systemes
LEFT JOIN categories
ON systemes.id_categories = categories.id_categories
LEFT JOIN maintenances
ON systemes_maintenances.id_maintenances = maintenances.id_maintenances;
And my PHP page looks like this at the moment:
<?php
/* #var $this SiteController */
$this->pageTitle=Yii::app()->name;
?>
<!--<h1>Welcome to <i><?php echo CHtml::encode(Yii::app()->name); ?></i></h1>
<p>Congratulations! You have successfully created your Yii application.</p>
<p>You may change the content of this page by modifying the following two files:</p>
<ul>
<li>View file: <code><?php echo __FILE__; ?></code></li>
<li>Layout file: <code><?php echo $this->getLayoutFile('main'); ?></code></li>
</ul>
<p>For more details on how to further develop this application, please read
the documentation.
Feel free to ask in the forum,
should you have any questions.</p>-->
<table>
<caption>État des systèmes</caption>
<tr>
<th>Catégorie</th>
<th>Nom</th>
<th>État actuel</th>
<th>Maintenance prévue</th>
<th>Début de l'incident</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
I want to display the results in the empty <td> </ td>.
Does anyone know how to do it without jQuery?
Since you are using Yii framework you can use CGridView component. This give nice set of features such as sorting, pagination and filtering.
Check following link for example usage.
http://www.yiiplayground.com/index.php?r=UiModule/dataview/gridView
To connect to the database and the connection was defined in the config yii. Must be added to the top of page two line of code.
$sql = 'querySQL';
$connection=Yii::app()->db;
$dataReader=$connection->createCommand($sql)->query();
their is a tutorial How to display data in php from Mysql.
Link: http://hightechnology.in/how-to-display-data-in-php-from-mysql/
may it will help you.
Try sth like this:
$query = "SELECT categories.name,
systemes.name,
systemes.etat_de_base,
maintenances.name,
maintenances.date,
maintenances.duree
FROM systemes_maintenances
LEFT JOIN systemes
ON systemes_maintenances.id_systemes = systemes.id_systemes
LEFT JOIN categories
ON systemes.id_categories = categories.id_categories
LEFT JOIN maintenances
ON systemes_maintenances.id_maintenances = maintenances.id_maintenances";
$count= Yii::app()->db->createCommand($query)->queryScalar();
$dataProvider = new CSqlDataProvider($query, array(
'totalItemCount'=>(int) $count,
'keyField' => 'SOME_UNIQUE_ID_FROM_THE_SQL',
'pagination'=>array( 'pageSize'=>30, ),
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'yourGrid',
'dataProvider'=> $dataProvider,
));
You will have to customize the grid yourself, so that it displays only what you need, but you can find this in the Yii documentation of CGridView
I am not so sure of the YII framework but this should definitely help you out.
Let the result of the mySql query be in a variable $result
now , start with while/for loop like this :
<?php
while ($row=mysqli_fetch_array($result)){
//Do something with the $row variable data
?>
<td>Some Data</td>
<td>echo $row["SomeColoumn1"];</td>
<td>echo $row["SomeColoumn2"];</td>
<td>echo $row["SomeColoumn3"];</td>
<td>echo $row["SomeColoumn4"];</td>
<?php
}
?>
This should print all the rows of the table as required and add the table and th parts before and after accordingly.

Categories