php echo not working in html - php

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.

Related

Foreach table output rows near each other

I have json html table in php, now i can't get it work that the output in the table will be near each other.
I am already few days busy with this, hope some one can help me.
<?php foreach($data2 as $row): ?>
<tr>
<td><?=$row['model'];?></td>
<td><?=$row['model2'];?></td>
</tr>
<?php endforeach;?>
My json output:
It looks like the JSON is not formatted for what you are trying to do. Every object has either modal or modal2, so the one that is not filled is undefined.
You should get the length of the longest list and use a normal for loop.
<?php ($index = 0; $index <= $lengthOfLongestList; $index++): ?>
<tr>
<td><?=$data2[index]['model'];?></td>
<td><?=$data2[index]['model2'];?></td>
</tr>
<?php endforeach;?>

PHP does not output isset array from Mysql. Although through print_r the array is displayed

The Mysql command is set correctly, since the data is displayed correctly via print_r($ads). I pack the resulting array into $ads
Catch id
$id = htmlentities($_GET['id']);
Query DB.
SELECT
rent.id,
rent.run,
rent.year,
FROM rent
WHERE rent.id = '.$id.'
ORDER BY rent.time_upload DESC');
But through the isset function, they are not shown, no errors are displayed, nothing, just a blank page.
I output the data like this
<?php if (isset($ads)): ?>
<h3>
<?=$ads['id'];?>
<?=$ads['run'];?>
<?=$ads['year'];?>
</h3>
<?php endif; ?>
short_tags are included in PHP.
Please help solve the problem.
You're not going deep enough. $ads['run'] doesn't exist, only $ads['3625']['run'] does. Try:
<?php
foreach( $ads as $ad ) {
?>
<h3>
<?=$ad['id'];?>
<?=$ad['run'];?>
<?=$ad['year'];?>
</h3>
<?php
}

Hyperlinked and creating a HTML Page that is nested in a table

So I have a html table that is automatically generated after passing a query to my database. I want to create a hyperlink within my html table to a page that will pull more detailed information from a Second Table.
I was thinking of using the Tablecell creator that pulls from the First Table, and modifying so that it would encompass the table's contents with hyperlink tags. I was thinking it would look like this.
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
echo <a href = "the reusable HTML Page">;
echo $row;
echo </a>;
}
Is my idea sound from a coding standpoint?
Firstly, echo's need to be in quotation marks " So that code wouldn't fire.
There are a few ways you can output HTML. The first is using echo's:
echo "Google";
Notice how I put a back-slash before hand? This is what is known as an escape. This puts the character after into a letter depending on what it escapes to. See php docs: http://php.net/manual/en/regexp.reference.escape.php (as my description of it was poor)
The other option would be to run out of php then join back on so to speak:
<?PHP
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
?>
<a href="abc">
<?PHP echo $row; ?>
</a>
<?PHP
}
However, this is not advised.
Edit:
Also, you can make your own table very simply:
<table>
<?PHP
foreach($stmt as $row){
?>
<tr>
<td>
<a href="abc"><?PHP echo $row[id]; ?>
</td>
</tr>
<?PHP
}
?>
</table>
See https://www.w3schools.com/html/html_tables.asp for more info.

Repeat link with $_GET

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>

Easiest way to Format MySQL Data to Next Column in PHP

I have my data being output to a span currently... this is how it looks:
Now, when i remove the span and place a div there i am given this output:
This is desired, but I want to set a height to my page and have the data show up in as little as 3 columns. How would I do this? I have searched everywhere online but can't seem to find anything that shows a solution.
I did read that some use javascript for the format but i am still clueless on even this option.
My desired output would look like this:
If you know how many items you want in a column then you can seperate them out into individual divs and then float those divs to the left to get them to be next to each other.
<div style='float:left'>
//Items go here
</div>
<div style='float:left'>
//Items go here
</div>
etc.
If you figure out how many items your query returned, say using mysql_num_rows() and divide by 3 you can tell how many to put in each column.
Also be sure to clear the floats afterwards, so like this:
<div style="clear:both"></div>
Sometimes this is necessary as there will be random issues if this is not put there.
What you are describing can be solved with styling only. You have several divs that must be displayed in columns. The easiest way is floating them to the left, and setting the width for 1/3 of the parent. If you want 4 columns, set the with to 1/4 of the parent, and so on.
<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>
Also as other answers mentioned, don't use duplicated ids. Always use classes. If you need to target each div individually, give it a unique id, such as "category_1", "category_2", and so on.
This should work
<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
<?php if($count == 0){ echo '<td>';} ?>
<span>
<?php echo $key; ?>
</span>
<?php if($total == $idxcount): ?>
</td>
<?php elseif($count == $limit): ?>
</td>
<?php $count=0; else: $count++; ?>
<?php endif; $idxcount++; ?>
<?php endwhile; ?>
</tr></table>

Categories