Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #15706
| Path | csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.php |
| Subject | Re: rawurlencode problem |
| Date | Tue, 20 Oct 2015 00:00:16 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 143 |
| Message-ID | <6395335.3oAvEOITrP@PointedEars.de> (permalink) |
| References | <d8kekpFmk8uU1@mid.individual.net> |
| Reply-To | Thomas 'PointedEars' Lahn <php@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1445292045 17759 eJwFwQERACAIA8BKAxmccRRZ/wj+c6VlVyQzKKq3wavJ5iAPzE11cXeNK9QOF/jm6cSaWh8TLREe (19 Oct 2015 22:00:45 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Mon, 19 Oct 2015 22:00:45 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-User-ID | eJwFwYEBwCAIA7CXVqB1noMI/59gQhdUK0QFhyO3no7jclqYSmja9M5GY35zFhiZ2HstInXrMk13zlc5D0XBFbo= |
| Cancel-Lock | sha1:tZqtHcY3xiVY4aj6VMqe9p3/VKs= |
| X-NNTP-Posting-Host | eJwFwYEBwDAEBMCVqPxjHCT2H6F3MCrHD8GDxbq+cIlVaVQrH2fzdEoEXODWUhfJDzs2t34OSRDY |
| Xref | csiph.com comp.lang.php:15706 |
Show key headers only | View raw
Derek Turner wrote:
> I'm using this:
>
> echo "<ol class=\"reader\">";
> while ($row = $result->fetch_assoc()):
> echo "<li class=\"reader\"> <a href = \"#".rawurlencode($row
> ['name'])."\">" . $row['name'] . "</a></li>\n\n";
> endwhile;
> echo "</ol>";
>
> to provide links for screen-readers to various points in my page
Please don’t. This is _not_ how one properly uses PHP in the 21st century.
Instead, in your controller code, use the equivalent of
$data = $result->fetch_all(MYSQLI_ASSOC);
and in your view template (which you include() or require() through your
controller code), the equivalent of
<ol class="reader">
<?php foreach ($data as $row): ?>
<li class="reader"><a
href="#<?= rawurlencode($row['name']) ?>"
><?= htmlspecialchars($row['name']) ?></a></li>
<?php endforeach; ?>
</ol>
thereby cleanly *separating business logic from presentation*.
Usually you want to define template variables instead of using $data. A
template engine can be useful there, but is not required if your controller
sets view "variables" instead; e.g.
$data = $result->fetch_all(MYSQLI_ASSOC);
$this->view->setVar('rows', $data);
and then
<ol class="reader">
<?php foreach ($this->rows as $row): ?>
<li class="reader"><a
href="#<?= rawurlencode($row['name']) ?>"
><?= htmlspecialchars($row['name']) ?></a></li>
<?php endforeach; ?>
</ol>
Also consider mysqli_result::fetch_object() which can convert records into
model objects directly. You can store sets of model objects in a template
variable, and then access object properties instead of array keys which has
several benefits thanks to the possibility of implicit setters and getters:
/*
* There is no other way but a loop with mysqli;
* PDO_Mysql has
*
* $links = $pdoStatement->fetchAll(PDO::FETCH_CLASS, 'Turner\\Link',
* $mapping);
*
* instead.
*/
while (($obj = $result->fetch_object('Turner\\Link', $mapping)))
{
$links[] = $obj;
};
$this->view->setVar('links', $links)
and then
<ol class="reader">
<?php foreach ($this->links as $link): ?>
<li class="reader"><a
href="#<?= rawurlencode($link->name) ?>"
><?= htmlspecialchars($link->name) ?></a></li>
<?php endforeach; ?>
</ol>
<http://php.net/manual/en/mysqli-result.fetch-object.php>
<http://php.net/manual/en/pdostatement.fetchall.php>
Use <?= … ?> instead of <?php echo … ?> if you use PHP 5.4+:
<http://php.net/manual/en/migration54.new-features.php>
The “li” elements probably do not need a CSS class each; you can write
stylesheets using the selector context
.reader li
to format them or their descendant elements instead.
> $row['name'] may and does contain white spaces in many records.
Given that users would want to use those references, it is prudent to
replace consecutive white space in fragment identifiers with a different
character that is easy for them to read and type, e.g. “-”. You can find
this all over the Web.
> The code works fine without the rawurlencode but fails validation
Presumably with the W3C Markup Validator.
> similarly this code lower down:
>
> while ($row = $result->fetch_assoc()):
> echo "<h2 id =\"" .rawurlencode($row['name']). "\">" . $row
> ['name'] . "</h2>";
> etc.
Same problem as above. Who is going to maintain this character mess?
> works without the rawurlencode but fails validation as 'id' must not
> contain white space.
IDs are _not_ URLs. Use htmlspecialchars() there instead.
<http://php.net/htmlspecialchars>
> SO the link href and the id are identical apart from the leading #
No, one is a *URI-reference*, the other is an *ID*.
> PROBLEM: this passes validation but DOES NOT WORK.
Of course it does not.
> What am I missing????
The HTML(5) Specification, a decent tutorial on modern design patterns like
MVC, and a keyboard with working Shift and Question Mark keys.
IOW: <http://www.catb.org/~esr/faqs/smart-questions.html>
--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
rawurlencode problem Derek Turner <frderek@suremail.je> - 2015-10-19 14:56 +0000
Re: rawurlencode problem Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-10-19 17:18 +0200
Re: rawurlencode problem Derek Turner <frderek@suremail.je> - 2015-10-19 15:48 +0000
Re: rawurlencode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-20 00:02 +0200
Re: rawurlencode problem Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-10-21 09:40 +0200
Re: rawurlencode problem "J.O. Aho" <user@example.net> - 2015-10-20 18:33 +0200
Re: rawurlencode problem "J.O. Aho" <user@example.net> - 2015-10-19 21:27 +0200
Re: rawurlencode problem Derek Turner <frderek@suremail.je> - 2015-10-19 19:43 +0000
Re: rawurlencode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-20 00:03 +0200
Re: rawurlencode problem "J.O. Aho" <user@example.net> - 2015-10-20 07:10 +0200
Re: rawurlencode problem "J.O. Aho" <user@example.net> - 2015-10-20 07:21 +0200
Re: rawurlencode problem Derek Turner <frderek@suremail.je> - 2015-10-20 08:29 +0000
Re: rawurlencode problem "J.O. Aho" <user@example.net> - 2015-10-20 18:29 +0200
Re: rawurlencode problem Arno Welzel <usenet@arnowelzel.de> - 2015-10-27 14:01 +0100
Re: rawurlencode problem Arno Welzel <usenet@arnowelzel.de> - 2015-10-27 14:04 +0100
Re: rawurlencode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-20 00:00 +0200
Re: rawurlencode problem Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-19 19:35 -0400
Re: rawurlencode problem Derek Turner <frderek@suremail.je> - 2015-10-20 08:34 +0000
Re: rawurlencode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-21 13:54 +0200
csiph-web